8

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

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.