Split Comma Value to Separate nodes/elements XSLT

Viewed 114

I have a scenario where In one of the XML fields Source application is sending a comma-separated value (as per their limitation), but our destination system accepts only a single value in a field (id ordering is not required). Can anyone please help in this case. Thanks in advance!

Current XML

<root>
    <order>
        <id>1</id>
        <value>RU</value>
    </order>
    <order>
        <id>2</id>
        <value>SA</value>
    </order>
    <order>
        <id>3</id>
        <value>MX,BR,US</value>
    </order>
    <order>
        <id>4</id>
        <value>IN,SL</value>
    </order>
</root>

Expected Output:

<root>
    <order>
        <id>1</id>
        <value>RU</value>
    </order>
    <order>
        <id>2</id>
        <value>SA</value>
    </order>
    <order>
        <id>3</id>
        <value>MX</value>
    </order>
    <order>
        <id>4</id>
        <value>IN</value>
    </order>
    <order>
        <id>3</id>
        <value>BR</value>
    </order>
    <order>
        <id>3</id>
        <value>US</value>
    </order>
    <order>
        <id>4</id>
        <value>SL</value>
    </order>
</root>
2 Answers

You could use the tokenize() function, like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="order[contains(value,',')]">
    <xsl:variable name="currentOrder" select="."/>
    <xsl:for-each select="tokenize(value,',')">
      <order>
        <id><xsl:value-of select="$currentOrder/id"/></id>
        <value><xsl:value-of select="."/></value>
      </order>
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>

See it working here: https://xsltfiddle.liberty-development.net/nbiE19Y

If you're on a system with - which relies on and therefore supports and only - you can say,

xmlstarlet sel \
-t -m 'root' \
    -e '{name()}' -n \
      -m 'order' \
        --var nid='id' \
        -m 'str:tokenize(value,",")' \
          -e 'order' \
            -c '$nid' \
            -e 'value' -v '.' -b \
          -b -n \
file.xml

where

  • -t is xsl:template matching /
  • -m maps to xsl:for-each
  • -e is xsl:element
  • -c is xsl:copy-of
  • -v is xsl:value-of
  • -b ends the current container (-m. -e, …); required only if any template option (after -t) follows
  • -n outputs a newline
  • str:tokenize is part of the EXSLT effort
  • indentation and line continuation chars are optional
  • your <o/><o/>-looking eyes will find a moment of relief

to output:

<root>
<order><id>1</id><value>RU</value></order>
<order><id>2</id><value>SA</value></order>
<order><id>3</id><value>MX</value></order>
<order><id>3</id><value>BR</value></order>
<order><id>3</id><value>US</value></order>
<order><id>4</id><value>IN</value></order>
<order><id>4</id><value>SL</value></order>
</root>

To list the generated XSLT code add -C before the -t option:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt str">
  <xsl:output omit-xml-declaration="yes" indent="no"/>
  <xsl:template match="/">
    <xsl:for-each select="root">
      <xsl:element name="{name()}">
        <xsl:value-of select="'&#10;'"/>
        <xsl:for-each select="order">
          <xsl:variable select="id" name="nid"/>
          <xsl:for-each select="str:tokenize(value,&quot;,&quot;)">
            <xsl:element name="order">
              <xsl:copy-of select="$nid"/>
              <xsl:element name="value">
                <xsl:call-template name="value-of-template">
                  <xsl:with-param name="select" select="."/>
                </xsl:call-template>
              </xsl:element>
            </xsl:element>
            <xsl:value-of select="'&#10;'"/>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="value-of-template">
    <xsl:param name="select"/>
    <xsl:value-of select="$select"/>
    <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
      <xsl:value-of select="'&#10;'"/>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Related