how to assign an attribute value to another attribute in a different Node in xslt 2.0 -


can me out on below requirement. id attribute value "123" assigned attribute ref="123" in node "cd11" . thanks, in advance

input xml

<publisher>     <name id="123">         <location>chicago</location>     </name>     <catalogue id="111" >         <cd11 id="222">             <title>empire burlesque</title>             <artist>bob dylan</artist>             <year>1985</year>         </cd11>     </catalogue> </publisher> 

output xml

<publisher>     <name id="123">         <location>chicago</location>     </name>     <catalogue id="111" >         <cd11 id="222" ref="123">             <title>empire burlesque</title>             <artist>bob dylan</artist>             <year>1985</year>         </cd11>     </catalogue> </publisher> 

transform : create new attribute "ref" in node "cd11" , attribute @name/id assigned @cd11/ref

this 1 possible way :

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="xml" indent="yes"/>      <xsl:template match="@* | node()">         <xsl:copy>             <xsl:apply-templates select="@* | node()"/>         </xsl:copy>     </xsl:template>      <xsl:template match="catalogue/cd11">       <xsl:copy>         <xsl:attribute name="ref">           <xsl:value-of select="parent::catalogue/preceding-sibling::name/@id"/>         </xsl:attribute>         <xsl:apply-templates select="@* | node()"/>       </xsl:copy>     </xsl:template> </xsl:stylesheet> 
  • <xsl:template match="@* | node()">... : identity template. template copies nodes , attributes applied to output xml, unchanged.

  • <xsl:template match="catalogue/cd11">... : template overrides identity template <cd11> element direct child of <catalogue> parent. template copies matched cd11 elements , create new attribute ref value taken id attribute of 'preceding' name element.


Comments