Search code examples
xmlxslttagsxslt-2.0adobe-indesign

Apply XSLT to add a tag


I'm exporting an XML file from InDesign. The text in this file contains superscripts referring to a specific grammatical concept; however, these superscripts are exported as text in the XML file. I need to write an XSLT so that when it's applied to the InDesign file it will simply add a little tag to the superscript.

This is how it's exported:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Content>
<PhraseNative aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="260.5">
<Phrase>    1.  Mark is1a playing2 videogames.</Phrase> 
</PhraseNative>
</Content>
</Root>  

This should be the final code.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Content>
<PhraseNative aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="260.5">
<Phrase>    1.  Mark is<tag>1a</tag> playing<tag>2</tag> videogames.</Phrase> 
</PhraseNative>
</Content>
</Root>

These tags would always appear whenever a number and a letter are the two or three last digits of a string. Sometimes it will be only a number. The output doesn't change at all. This is just so the tags are not lost when exported back into a web page.


Solution

  • You tagged the question with XSLT 2.0, so here's a 2.0 option.

    Note: I had to add a dummy xmlns for the aid prefix.

    Also, you'll most likely need to refine the regex, but this should get you started.

    XML Input

    <Root>
      <Content>
        <PhraseNative aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="260.5" xmlns:aid="somexmlns">
          <Phrase>    1.  Mark is1a playing2 videogames.</Phrase> 
        </PhraseNative>
      </Content>
    </Root>
    

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:aid="somexmlns">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Phrase">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:analyze-string select="." regex="([a-z]+)([0-9]+[a-z]*)">
            <xsl:matching-substring>
              <xsl:value-of select="regex-group(1)"/>
              <tag>
                <xsl:value-of select="regex-group(2)"/>    
              </tag>
            </xsl:matching-substring>
            <xsl:non-matching-substring><xsl:value-of select="."/></xsl:non-matching-substring>
          </xsl:analyze-string>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    XML Output

    <Root>
       <Content>
          <PhraseNative xmlns:aid="somexmlns" aid:table="cell" aid:crows="1" aid:ccols="1"
                        aid:ccolwidth="260.5">
             <Phrase>    1.  Mark is<tag>1a</tag> playing<tag>2</tag> videogames.</Phrase>
          </PhraseNative>
       </Content>
    </Root>
    

    Tested using Saxon-HE 9.3.