How to pass variable name inside for-each loop in xslt

386 Views Asked by At

I'm new to Xslt I'm reading xml data using xslt. for example

<head>
    <root>
        <data>
            <Name>xxx</Name>      
            <age>10</age>
        </data>
        <data>
            <Name>yyy</Name>      
            <age>20</age>
        </data>
        <data>
            <Name>zzz</Name>      
            <age>15</age>
        </data>
    </root>
</head>

When I tried use below xslt

<xsl:template match="head">
  <xsl:element name="Root1">
      <xsl:for-each select="//head/root/data">
         <xsl:element name="name">
            <xsl:value-of select="$select_Name"/>
         </xsl:element>
         <xsl:element name="age">
            <xsl:value-of select="$select_age"/>
         </xsl:element>
      </xsl:for-each>
  </xsl:element>
</xsl:template>

My c# code is

  XslTransform xsl=new XslTransform();
  xsl.Load("xsltfile");
  xsl.Transform("inputxml.xml","outputxml.xml");

I'm not getting proper output when i use the above xslt file. whereas when I give the xpath of the element to a variable in another xslt file and include the xslt into this xslt and pass variable name instead of hardcoded value <xsl:value-of select="$select_Name"/> like this whereas $name is declared as <xsl:variable name="select_Name" select="//head/root/data/Name"/> im getting the value of name for the field of Age. Anyone faced this kind of issue? or can you share how to pass the variable name instead of element name for the value inside for-each loop?

0

There are 0 best solutions below