3 Replies Latest reply on Nov 27, 2012 1:25 AM by SimonLinden

    Extracting XML attributes

    SimonLinden

      Hi!

      I'm working on a big XML document that I will import into inDesign. Many tags have the following structure:

      <verse value="1" chapter="1">...some text...</verse>

       

      I need to extract the numbers of the 'value' and 'chapter' attributes to add them in the text and style them with character styles. Can this be done with inDesign or with some simple script? Or do I have to use XSLT? I have absolutely no experience of XSLT but I'll be looking into it. I'm assuming this should be quite easy for someone who knows these things though so if someone could give me a hand I'd be grateful.

        • 1. Re: Extracting XML attributes
          SimonLinden Level 1

          Ok, I've made some progress. I managed to put together the following XSL code, which creates tags based on the attribute types and puts the values as tag content:

           

          <?xml version="1.0" encoding="ISO-8859-1"?>

          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

           

          <xsl:template match="verse">

              <xsl:if test="@value = 1">

                        <chapter-number><xsl:value-of select="@chapter"/></chapter-number>

              </xsl:if>

              <verse-number><xsl:value-of select="@value"/></verse-number>

              <xsl:apply-templates/>

          </xsl:template>

           

          </xsl:stylesheet>

           

          The problem is that it removes all other tags. How can I keep this effect and still keep all the other tags?

           

          My other problem is that inDesign won't allow me to use this XSLT before import. It just says "could not write data to output". It works when I open the XML in a browser though so the code seems valid.

          • 2. Re: Extracting XML attributes
            absqua Level 4

            It doesn't look to me like your stylesheet would produce good xml.

             

            To convert all attributes to child elements, you could use this:

             

            <?xml version="1.0" encoding="UTF-8"?>
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
            
                <xsl:template match="/">
                    <xsl:apply-templates select="*"/>
                </xsl:template>
            
                <xsl:template match="node()">
                    <xsl:copy>
                        <xsl:for-each select="@*">
                            <xsl:element name="{name()}">
                                <xsl:value-of select="."/>
                            </xsl:element>
                        </xsl:for-each>
                        <xsl:apply-templates select="node()"/>
                    </xsl:copy>
                </xsl:template>
            
            </xsl:stylesheet>
            

             

            If you only need to convert some attributes on some elements, you'd have to take a different approach.

             

            Jeff

            1 person found this helpful
            • 3. Re: Extracting XML attributes
              SimonLinden Level 1

              Thanks, that's quite helpful. I gave up on XSL because I couldn't get it to do what I wanted, but this code works. I only have to replace the asterisk in the for-each tag with the name of the attirbute I want to affect.

               

              I've used Perl with regexp instead to prepare the document for inDesign but using XSL makes more sense I think.