Skip to main content
Rsfl
Inspiring
August 9, 2018
Question

Bulk convertion of xrefs to text

  • August 9, 2018
  • 1 reply
  • 321 views

Hi all,

we need to convert a large amount of cross references to plain text. Are you aware of a script that can do this, or do you have any recommendations? The xrefs partially reference the titles of the target chapters, but partially they also contain a manually written link text.

This topic has been closed for replies.

1 reply

FabianK007
Known Participant
August 14, 2018

this might work for one file you opened:

var doc = app.FirstOpenDoc;

//get the map of the xml file:

var tempxmlMap = ReadXML(doc.Name)

//gather xrefs from the map:

var xmlXRefs = tempxmlMap..xref;

//loop through the xrefs found in the map:

for( var i = 0; i < xmlXRefs.length( ); i++ ) {

    var sHref = xmlXRefs.@href.toString( );

    //PartofsHref = sHref.replace(/#.*/g, "") //use regex magic to extract the part of the xref you need

    var xrefList=new File("D:\\xref.txt");

    if(!xrefList.exists) {

                xrefList.open('w');

            } else {

                xrefList.open('a');

            }

    xrefList.writeln(doc.Name + "\trefers to:\t" + sHref); //or PartofsHref instead of sHref

    xrefList.close();

}

//function to get check validity and get the map:

function ReadXML(filename) {

    var xmlMap;

    var oFile = File(filename);

    if( oFile.open( 'r' ) ){

        sContents = oFile.read( oFile.length );

        oFile.close( );

        try {

            xmlMap = new XML( sContents );

        }

        catch(err) {

            $.writeln(filename + "\t" + err.toString().replace("\n", " "))

            return false

        }

    }

    return xmlMap;

}

you may adapt the script to make it loop through several files.