Search code examples
xmlxsltxpathxslt-2.0xslt-3.0

Creating XML nodes based on XPATH


I have a requirement to add XML nodes using XPATH. I got the below XSLT, from another thread in stackoverflow, which does the job. But I am facing one issue where if we are not maintaining the order of XPATH in XSLT then it is creating same XML tags multiple times. Please refer below sample XML and XSLT to understand it better. Kindly help to resolve this issue.

Input XML:

<header>
  <txCtry>SG</txCtry>
  <msgId>b626c6be4a724f8aa92dcba1b4e07cf0</msgId>
</header>
<data>
  <txCtry>SG</txCtry>
  <txCityCd>SIN</txCityCd>
</data>

XSLT:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vPop" as="element()*">
<item path="/header/txCtryyyy">F</item>
<item path="/header/btchBookg">ddddddddddddd</item>
<item path="/data/txSttlmInf/instgRmbrsmntAgt/BICFIIIIII">FXZ9200136548878</item>
<item path="/header/prcgSubSts">ooooooooooooooo</item>
 </xsl:variable>

 <xsl:template match="/">
  <xsl:sequence select="my:subTree($vPop/@path/concat(.,'/',string(..)))"/>
 </xsl:template>

 <xsl:function name="my:subTree" as="node()*">
  <xsl:param name="pPaths" as="xs:string*"/>

  <xsl:for-each-group select="$pPaths"
    group-adjacent=
        "substring-before(substring-after(concat(., '/'), '/'), '/')">
    <xsl:if test="current-grouping-key()">
     <xsl:choose>
       <xsl:when test=
          "substring-after(current-group()[1], current-grouping-key())">
         <xsl:element name=
           "{substring-before(concat(current-grouping-key(), '['), '[')}">

          <xsl:sequence select=
            "my:subTree(for $s in current-group()
                         return
                            concat('/',substring-after(substring($s, 2),'/'))
                             )
            "/>
        </xsl:element>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="current-grouping-key()"/>
       </xsl:otherwise>
     </xsl:choose>
     </xsl:if>
  </xsl:for-each-group>
 </xsl:function>
</xsl:stylesheet>

Transformed XML:

<header>
   <txCtryyyy>F</txCtryyyy>
</header>
<data>
   <txSttlmInf>
      <instgRmbrsmntAgt>
         <BICFIIIIII>FXZ9200136548878</BICFIIIIII>
      </instgRmbrsmntAgt>
   </txSttlmInf>
</data>
<header>
   <btchBookg>ddddddddddddd</btchBookg>
</header>
<data>
   <txSttlmInf>
      <instgRmbrsmntAgt>
         <BICFHHHH>FXZ92001365kkkkkkkk48878</BICFHHHH>
      </instgRmbrsmntAgt>
   </txSttlmInf>
</data>
<header>
   <prcgSubSts>ooooooooooooooo</prcgSubSts>
</header>

Here we can see header and data tags getting created multiple times.

Expected output:

<header>
   <txCtryyyy>F</txCtryyyy>
   <btchBookg>ddddddddddddd</btchBookg>
   <prcgSubSts>ooooooooooooooo</prcgSubSts>
</header>
<data>
   <txSttlmInf>
      <instgRmbrsmntAgt>
         <BICFIIIIII>FXZ9200136548878</BICFIIIIII>
         <BICFHHHH>FXZ92001365kkkkkkkk48878</BICFHHHH>
      </instgRmbrsmntAgt>
   </txSttlmInf>
</data>

Solution

  • Try to use group-by instead of group-adjacent in the function where you have <xsl:for-each-group select="$pPaths" group-adjacent="substring-before(substring-after(concat(., '/'), '/'), '/')">, try to use <xsl:for-each-group select="$pPaths" group-by="substring-before(substring-after(concat(., '/'), '/'), '/')">.