Search code examples
xsltmiddlewaresoaassignbpel

How do i split and assign values in nested nodes using Assign/XSLT in BPEL SOA?


Input-:

<input>1,2,3</input>

1,2,3 values separated with ",".

Required output-:

<ABC>
  <AB>
    <result>1</result>
  </AB>
  <AB>
   <result>2</result>
  </AB>
  <AB>
   <result>3</result>
  </AB>
</ABC>

Solution

  • Here is an XSLT 1.0 solution. It makes use of a recursive named template. I'm unsure this could be done using a match="text()" template, thus avoiding the parameters passing.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    
      <xsl:template name="split">
        <xsl:param name="str" />
    
        <xsl:if test="contains($str, ',')">
          <AB>
            <result><xsl:value-of select="substring-before($str, ',')" /></result>
          </AB>
          <xsl:call-template name="split">
            <xsl:with-param name="str" select="substring-after($str, ',')" />
          </xsl:call-template>
        </xsl:if>
      </xsl:template>
    
      <xsl:template match="/">
        <ABC>
          <xsl:call-template name="split">
            <xsl:with-param name="str" select="concat(input/text(), ',')" />
          </xsl:call-template>
        </ABC>
      </xsl:template>
    
    </xsl:stylesheet>
    

    XSLT 2 has a tokenize function which allows you to do this more concisely :

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    
      <xsl:template match="/">
        <ABC>
          <xsl:for-each select="tokenize(input, ',')">
            <AB>
              <result><xsl:value-of select="." /></result>
            </AB>
          </xsl:for-each>
        </ABC>
      </xsl:template>
    
    </xsl:stylesheet>