When the XSLT for each is in a loop. How to add an attribute or node to this XML based on a different XML value. USING XSLT

Can someone help me solve this problem?

I have XML and value filtering based on some condition. Storing filtered xml in a variable. When filtering a condition, I try to add an attribute or node to the filtered xml, but it does not work for me.

XML input:

<root> <a id="13"> <b>XXX1</b> <c>YYY1</c> </a> <a id="2"> <b>XXX2</b> <c>YYY2</c> </a> <a id="15"> <b>XXX3</b> <c>YYY3</c> </a> <a id="37"> <b>XXX4</b> <c>YYY4</c> </a> <a id="51"> <b>XXX5</b> <c>YYY5</c> </a> </root> 

Another XML that is stored in a variable called "data" (this is for filtering):

 <sample> <con id="37" order="1"/> <con id="13" order="2"/> <con id="51" order="3"/> <con id="2" order="4"/> <con id="15" order="5"/> </sample> 

Using XSLT, I am trying to filter and add an element this way.

 <xsl:variable name="filteredData"> <newroot> <xsl:for-each select="/root/a[@id > 14]"> <xsl:if test="msxsl:node-set($data)/sample/con[@id = current()/@id]/@id = current()/@id"> <xsl:element name="order"> <xsl:value-of select="msxsl:node-set($data)/sample/con[@id = current()/@id]/@order"/> </xsl:element> </xsl:if> </xsl:for-each> </newroot> </xsl:variable> 

OUTPUT XML (ie, the variable "filterData" must contain below XML):

  <newroot> <a id="15"> <b>XXX3</b> <c>YYY3</c> <order>5</order> </a> <a id="37"> <b>XXX4</b> <c>YYY4</c> <order>1</order> </a> <a id="51"> <b>XXX5</b> <c>YYY5</c> <order>3</order> </a> </newroot> 
+4
source share
2 answers

Try using a lookup table with a key function, as in this example. Tip: XSLT lookup table

I managed to get the following snippet to create an XML document that matched your output above. The filtering data in xslt below is downloaded from a separate document, but it is easy to adapt.

 <xsl:key name="id-lookup" match="con" use="@id"/> <xsl:variable name="id-top" select="document('<lookup file>')/sample"/> <xsl:template match="root"> <newroot> <xsl:for-each select="a[@id > 14]"> <xsl:copy> <xsl:copy-of select="@*|node()"/> <xsl:element name="order"> <xsl:apply-templates select="$id-top"> <xsl:with-param name="curr-label" select="."/> </xsl:apply-templates> </xsl:element> </xsl:copy> </xsl:for-each> </newroot> </xsl:template> <xsl:template match="sample"> <xsl:param name="curr-label"/> <xsl:value-of select="key('id-lookup', $curr-label/@id)/@order"/> </xsl:template> 
+1
source

Based on the inputs, now I have tried and implemented a different presentation form.

New XSLT code:

 <xsl:variable name="filteredData"> <newroot> <xsl:for-each select="/root/a[@id > 14]"> <xsl:copy> <xsl:copy-of select="@*|node()"/> <xsl:element name="Order"> <xsl:choose> <xsl:when test="msxsl:node-set($data)/sample/con[@id = current()/@id]/@id = current()/@id"> <xsl:value-of select="msxsl:node-set($data)/sample/con[@id = current()/@id]/@order"/> </xsl:when> <xsl:otherwise> <xsl:text>&#160;</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:copy> </xsl:for-each> </newroot> </xsl:variable> 
+1
source

All Articles