Indy2
Indy2

Reputation: 13

XSLT 1.0 Split comma seperated string into named nodes

A customer supplied XML contains the delivery address as a comma separated string, I would like to split this string into named nodes using XSLT 1.0.

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <text>
     Company, Streetaddress 20, 1234 AA, City
   </text>
</root>

Output

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <text>
    <COMPANY>Company</COMPANY>
    <ADDRESS>Streetaddress 20</ADDRESS>
    <ZIPCODE>1234 AA</ZIPCODE>
    <CITY>City</CITY>
  </text>
</root>

I tried several recursive templates for XSLT 1.0 which do a fine job splitting but the resulting nodes are identically named.

If possible, how can this be achieved using XSLT 1.0?

Upvotes: 0

Views: 2844

Answers (3)

Indy2
Indy2

Reputation: 13

As the supplied XML only had the address as single node (Streettaddress 20) I added a second variable to split the address string into streetaddress and housenumber.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
  <xsl:template match="text">
   <root>
      <COMPANY>
        <xsl:value-of select="normalize-space(substring-before(., ','))"/>
      </COMPANY>
        <xsl:variable name="s1" select="substring-after(., ',')"/>
        <xsl:variable name="address_temp" select="normalize-space(substring-before($s1, ','))"/>
      <ADDRESS>
        <xsl:value-of select="normalize-space(substring-before($address_temp, ' '))"/>
      </ADDRESS>
      <HOUSENUMBER>
        <xsl:value-of select="normalize-space(substring-after($address_temp, ' '))"/>
      </HOUSENUMBER>
        <xsl:variable name="s2" select="substring-after($s1, ',')"/>
      <ZIPCODE>
        <xsl:value-of select="normalize-space(substring-before($s2, ','))"/>
      </ZIPCODE>
      <CITY>
        <xsl:value-of select="normalize-space(substring-after($s2, ','))"/>
      </CITY>
    </root>
  </xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="UTF-8"?>
   <root>
   <COMPANY>Company</COMPANY>
   <ADDRESS>Streetaddress</ADDRESS>
   <HOUSENUMBER>20</HOUSENUMBER>
   <ZIPCODE>1234 AA</ZIPCODE>
   <CITY>City</CITY>
</root>

Upvotes: 1

Jukka Matilainen
Jukka Matilainen

Reputation: 10198

For the fun of it, here is a generic version using a recursive template.

<xsl:template match="text">
  <xsl:copy>
    <xsl:call-template name="parse-comma-separated">
      <xsl:with-param name="elements" select="'COMPANY,ADDRESS,ZIPCODE,CITY'"/>
      <xsl:with-param name="text" select="."/>
    </xsl:call-template>
  </xsl:copy>
</xsl:template>

<xsl:template name="parse-comma-separated">
  <xsl:param name="elements"/>
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="contains($elements, ',')">
      <xsl:element name="{normalize-space(substring-before($elements, ','))}">
        <xsl:value-of select="normalize-space(substring-before($text, ','))"/>
      </xsl:element>
      <xsl:call-template name="parse-comma-separated">
        <xsl:with-param name="elements" select="substring-after($elements, ',')"/>
        <xsl:with-param name="text" select="substring-after($text, ',')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:element name="{normalize-space($elements)}">
        <xsl:value-of select="normalize-space($text)"/>
      </xsl:element>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Upvotes: 2

Jukka Matilainen
Jukka Matilainen

Reputation: 10198

Does it have to be a recursive template? How about a straight-forward chain of substring-before and substring-after like this:

  <xsl:template match="text">
    <xsl:copy>
      <COMPANY>
        <xsl:value-of select="normalize-space(substring-before(., ','))"/>
      </COMPANY>
      <xsl:variable name="s1" select="substring-after(., ',')"/>
      <ADDRESS>
        <xsl:value-of select="normalize-space(substring-before($s1, ','))"/>
      </ADDRESS>
      <xsl:variable name="s2" select="substring-after($s1, ',')"/>
      <ZIPCODE>
        <xsl:value-of select="normalize-space(substring-before($s2, ','))"/>
      </ZIPCODE>
      <CITY>
        <xsl:value-of select="normalize-space(substring-after($s2, ','))"/>
      </CITY>
    </xsl:copy>
  </xsl:template>

Upvotes: 3

Related Questions