i have to duplicate the xml payload into as many xml payloads based on a specific id, 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 the output as
<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>
is this possible
Update: The below answer that was given is working fine, but there's a small catch I failed to mention. If the userid is the same and it's repeating, then the same xml payload should be displayed. For this I tried the following to get the unique elements of userid
<xsl:param name="userId" select="ns0:UserId[generate-id(.)=generate-id(key('k', ns0:UserId)[1])]"/>
but this is not working and also tried using above
..[generate-id(.)=generate-id(key('k', ns0:UserId)[1])]
at template level also it is not working
Am I missing something?
Update : i made a small modification to the above code, instead of working at xsl:param, i have used it at xsl:apply-template
before modification (provided as answer to me) <xsl:apply-templates select="//ns2:Details/ns2:UserId"/> after modification <xsl:apply-templates select="//ns2:Details/ns2:UserId[generate-id(.)=generate-id(key('myUserId', .)[1])]"/>
my mistake i was using 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:template>
<xsl:template match="//ns2:Details"> <xsl:param name="userId" select="ns2:UserId"/> <ns2:Details> <xsl:copy-of select="key('k', $userId)[1]"/> <!-- displays UserId values--> <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:apply-templates> </xsl:template>
Please, validate it. this too is working for me...
UserId
generate otherDetails
? Does my answer work fine for you? – Emiliano Poggixsl:for-each
, no parameters) solution so far. :) Also, provided is a brief explanation of the key moments in the solution. – Dimitre Novatchev