I need to duplicate the xml payload on so many useful xml payloads based on a specific identifier, e.g. userid
<ns2:Details xmlns:ns2="ns"> <ns2:var1>AA0511201143</ns2:var1> <ns2:var2>PARCEL</ns2:var2> <ns2:var3>04/04/2011</ns2:var3> <ns2:var4>Organization</ns2:var4> <ns2:UserId>46</ns2:UserId> <ns2:UserId>237</ns2:UserId> </ns2:Details>
I need a conclusion like
<ns2:Details> <ns2:var1>AA0511201143</ns2:var1> <ns2:var2>PARCEL</ns2:var2> <ns2:var3>04/04/2011</ns2:var3> <ns2:var4>Organization</ns2:var4> <ns2:UserId>46</ns2:UserId> </ns2:Details> <ns2:Details> <ns2:var1>AA0511201143</ns2:var1> <ns2:var2>PARCEL</ns2:var2> <ns2:var3>04/04/2011</ns2:var3> <ns2:var4>Organization</ns2:var4> <ns2:UserId>237</ns2:UserId> </ns2:Details>
it's possible
Update: The answer below works fine, but there is a small catch that I did not mention. If the user ID is the same and it repeats, then the same xml payload should be displayed. For this, I tried the following to get unique userid elements
<xsl:param name="userId" select="ns0:UserId[generate-id(.)=generate-id(key('k', ns0:UserId)[1])]"/>
but this does not work, and also tried to use the above
..[generate-id(.)=generate-id(key('k', ns0:UserId)[1])]
at the template level also does not work
Am I missing something?
Update : I made a small modification of the above code, instead of working with xsl: param, I used it in xsl: apply-template
before the change (provided as an answer to me) <xsl: apply-templates select = "// ns2: Details / ns2: UserId" / "> after the modification <xsl: apply-templates select =" // ns2: Details / ns2 : UserId [generate-id (.) = Generate-id (key ('myUserId' ,.) [1])] "/">
In my error, I used ns2: userid instead of "."
full xsl code ---
<xsl: output method = "xml" indent = "yes" / "> <xsl: key name =" k "match =" ns2: UserId "use =" text () "/"> <xsl: key name = " myUserId "match =" ns2: UserId "use =". " / "> <xsl: template match =" / "> <NS2: Root> <xsl: apply-templates select =" // ns2: Details / ns2: UserId [generate-id (.) = generate-id (key ( 'myUserId' ,.) [1])] "/"> </ NS2: Root> </ XSL: pattern>
<xsl: template match = "// ns2: Details"> <xsl: param name = "userId" select = "ns2: UserId" / "> <NS2: Details> <xsl: copy-of select =" key (' k ', $ userId) [1] "/"> <! - displays values ββUserId β <xsl: copy-of select = "./* [name ()! = 'ns2: UserId']" / "> <! - displays other values βββ </ NS2: Details> </ XSL: template>
<xsl: template match = "ns2: UserId"> <xsl: apply-templates select = ".."> <xsl: with-param name = "userId" select = "." / "> </ XSL: templates apply-> </ XSL: templates>
Please confirm it. this works for me too ...