Part 1
So, let's say if the country identifier is 32, then it should add the attribute country = 32 for the node employee.
This conversion is :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="Employee[countryid=32]"> <Employee countryid="{countryid}"> <xsl:apply-templates select="@*|node()"/> </Employee> </xsl:template> </xsl:stylesheet>
when applied to the provided XML document :
<Employees> <Employee> <countryid>32</countryid> <id name="id">1</id> <firstname >ABC</firstname> <lastname >XYZ</lastname> </Employee> <Employee> <countryid>100</countryid> <id name="id">2</id> <firstname >ddd</firstname> <lastname >ggg</lastname> </Employee> </Employees>
creates the desired, correct result :
<Employees> <Employee countryid="32"> <countryid>32</countryid> <id name="id">1</id> <firstname>ABC</firstname> <lastname>XYZ</lastname> </Employee> <Employee> <countryid>100</countryid> <id name="id">2</id> <firstname>ddd</firstname> <lastname>ggg</lastname> </Employee> </Employees>
Explanation
An identification rule is used to copy each node as-is . Using and redefining an identity rule (template) is the most fundamental and powerful XSLT design pattern.
There is only one template that overrides the identification rule for specific nodes - Employee , which has a child countryid with a string value (converted to a number) 32. This template adds the countryid attribute to the Employee element and applies templates to renew the identity rule and copy everything else as it is.
Part 2.
We can also skip countryid as a comma-separated values so that I can pass 32 100, and then we should add an attribute for all matching nodes
This conversion is :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="pIds" select="'32,100'"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="Employee"> <Employee> <xsl:if test= "contains(concat(',',$pIds,','), concat(',',countryid,',') )"> <xsl:attribute name="countryid"> <xsl:value-of select="countryid"/> </xsl:attribute> </xsl:if> <xsl:apply-templates select="@*|node()"/> </Employee> </xsl:template> </xsl:stylesheet>
when applied to the same XML document (above), it creates the desired, correct result :
<Employees> <Employee countryid="32"> <countryid>32</countryid> <id name="id">1</id> <firstname>ABC</firstname> <lastname>XYZ</lastname> </Employee> <Employee countryid="100"> <countryid>100</countryid> <id name="id">2</id> <firstname>ddd</firstname> <lastname>ggg</lastname> </Employee> </Employees>