Search code examples
xmlextendscriptadobe-indesign

How to count a specific tags in xml file?


XML:

<chapter>
<openr><title><num aid:pstyle="_CN">1</num></title></openr>
<media.block id="fig001"><media type="image"/><caption aid:pstyle="Caption"><num>FIG. 1.1&#x2002;</num><para><txt>some text</txt></para></caption></media.block>
<txt>some</txt>
<media.block id="fig002"><media type="image"/><caption aid:pstyle="Caption"><num>FIG. 1.2&#x2002;</num><para><txt>some text</txt></para></caption></media.block>
</chapter>

I need to extract the count of <media.block(to get the no.of figures in a document) and the content of <num aid:pstyle="_CN">1</num>(here ans=1).

I don't know how to access XML element using extendscript. Atleast a hint would be more than enough


Solution

  • This can be done in a lot of ways. One is as simple as using the built-in XML object in Javascript:

    xmlFile = File ('/test.xml');
    xmlFile.open();
    var myString = xmlFile.read();
    xmlFile.close();
    
    myXml = new XML ();
    myXml = XML(myString);
    
    media_block = myXml.xpath("//media.block");
    
    alert (media_block.length());