Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use parameter or variable values as node name?

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

like image 297
user1859463 Avatar asked Nov 28 '12 10:11

user1859463


1 Answers

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>
like image 60
xiaoyi Avatar answered Oct 02 '22 19:10

xiaoyi