2

I'm working in a database that doesn't really support subelements. To get around this, we've been using squiggly brackets }} in a string of values to indicate separations between subelements. When we export to xml, it looks something like this:

<geographicSubject>
   <data>Mexico }} tgn }} 123456</data>
   <data>Mexico City }} tgn }} 7891011</data>
   <data>Main Street }} tgn }} 654321</data>
</geographicSubject>

My question: how do I create our XSLT so that it splits the strings in <data> into separate uniquely named subelements like this:

<data>
   <location>Mexico</location>
   <source>tgn</source>
   <id>123456</id>
</data>

The first }} indicates the start of "source", the second }} indicates the start of "id". Thanks to anyone willing to help!

Taryn
  • 242,637
  • 56
  • 362
  • 405

1 Answers1

4

Compose a tokenizer:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:e="http://localhost">
    <e:e>location</e:e>
    <e:e>source</e:e>
    <e:e>id</e:e>
    <xsl:variable name="vElement" select="document('')/*/e:*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="data/text()" name="tokenizer">
        <xsl:param name="pString" select="string()"/>
        <xsl:param name="pPosition" select="1"/>
        <xsl:if test="$pString">
            <xsl:element name="{$vElement[$pPosition]}">
                <xsl:value-of
                 select="normalize-space(
                            substring-before(concat($pString,'}}'),'}}')
                         )"/>
            </xsl:element>
            <xsl:call-template name="tokenizer">
                <xsl:with-param name="pString"
                                select="substring-after($pString,'}}')"/>
                <xsl:with-param name="pPosition" select="$pPosition + 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Output:

<geographicSubject>
    <data>
        <location>Mexico</location>
        <source>tgn</source>
        <id>123456</id>
    </data>
    <data>
        <location>Mexico City</location>
        <source>tgn</source>
        <id>7891011</id>
    </data>
    <data>
        <location>Main Street</location>
        <source>tgn</source>
        <id>654321</id>
    </data>
</geographicSubject>