How to use parameter or variable values as node name?

parameters, variables, xslt

Solution

A wild guess, let me know if it works:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="#all">

<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
   <Output>
      <xsl:value-of select="//*[name()=$nodename]" />
   </Output>     
</xsl:template>

</xsl:stylesheet>

Problem

I am trying to use the value of a parameter or variable as a node name inside a value-of select but so far failed.. So my XML is as below. ``` <Data> <Name>John Smith</Name> <Date>28112012</Date> <Phone>iphone</Phone> <Car>BMW</Car> </Data> ``` And my incomplete xslt looks like below. ``` <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="#all"> <xsl:param name="nodename" select="'Name'"/> <xsl:template match="/Data"> <Output> <xsl:value-of select="{$nodename}"/> </Output> </xsl:template> </xsl:stylesheet> ``` Ideally I want the out put to be ``` <Output>John Smith</Output> ``` Is there any way I can do this using XSLT? I want to be able to select appropriate node based on a users choice. Thanks SK

Original source