1 Reply Latest reply on Mar 27, 2013 3:59 AM by [Jongware]

    Need help with XSL

    SimonLinden Level 1

      Hi!

      When exporting to XML from InDesign you have the option to apply an XSL-file to transform the output. I assume this still means the output will be XML? The problem is that when I try to create some simple XSL just to figure out how it works it removes all the tags so that it's no longer XML. I imagine there's something wrong with my XSL and I've been trying to figure out how to do it but for some reason I just can't get my head around XSL.

       

      So, for starters, could someone tell me how to make an XSL that adds linebreaks before the tags that represent new paragraphs (named <p>,<s> and <mt>, paths '/Root/Story/p' etc)? This will make the XML easier to read and maybe from there I can figure how to do the other things I need.

        • 1. Re: Need help with XSL
          [Jongware] Most Valuable Participant

          SimonLinden wrote:

           

          When exporting to XML from InDesign you have the option to apply an XSL-file to transform the output. I assume this still means the output will be XML? The problem is that when I try to create some simple XSL just to figure out how it works it removes all the tags so that it's no longer XML. I imagine there's something wrong with my XSL and I've been trying to figure out how to do it but for some reason I just can't get my head around XSL.

           

          The output of XSLT *can* be XML, but also plain text, HTML, or something entirely different (probably even something as complicated as RTF). But yes: if you want to import XML into InDesign, you have to make sure the output of your XSLT stylesheet is correct XML again.

           

          XSLT is a weird processing language It took me a while to get my head around it as well. By default, XSLT does *not* copy existing tags into its output -- you need to tell it to! To get an exact structure unmodified back into your output, you need to use the "XSLT Identity Transform" (http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT).

           

          If you want to amend to existing XML (rather than entirely rewriting the structure), you can start with the Identity Transform and add the custom template matches to it.

          So, for starters, could someone tell me how to make an XSL that adds linebreaks before the tags that represent new paragraphs (named <p>,<s> and <mt>, paths '/Root/Story/p' etc)? This will make the XML easier to read and maybe from there I can figure how to do the other things I need.

           

          You can try something like this:

           

            <xsl:template match="p|s|mt">
             <xsl:text>
          </xsl:text><xsl:copy>
               <xsl:apply-templates select="@*" />
             </xsl:copy>
            </xsl:template>
          

           

          It's fairly simple, as far as XSLT goes: the default rule in the identity transform copies an entire structure verbatim; adding this rule first inserts a hard return (the text inside the <xsl:text>..</xsl:text> tags) and then continues to 'copy' the original tag, plus its attributes (the 'select="@*"' rule), for the set of tags that it matches.