variables - 如何使用参数或变量值作为节点名称?

标签 variables xslt parameters

我试图使用参数或变量的值作为值选择内的节点名称,但到目前为止失败了..

所以我的 XML 如下。

<Data>
 <Name>John Smith</Name>
 <Date>28112012</Date>
 <Phone>iphone</Phone>
 <Car>BMW</Car>
</Data>

我的不完整 xslt 如下所示。

<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>

理想情况下我希望输出是

<Output>John Smith</Output>

有什么方法可以使用 XSLT 来做到这一点吗? 我希望能够根据用户的选择选择适当的节点。

谢谢

SK

最佳答案

一个疯狂的猜测,请告诉我它是否有效:

<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>

关于variables - 如何使用参数或变量值作为节点名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13602967/

相关文章:

xml - 带有动态标题的网页

c# - xsl 格式日期(子字符串 + 连接?)

c - 出现错误 : "variable...is uninitialized when used within its own initialization" in C

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

java - Double 未存储正确的值

css - XSL :when - Apply multiple attributes

java - 编译java变量错误

sql - 是否有数组的 SQL 参数绑定(bind)?

delphi - 如何声明基于泛型类型的指针?

linux - git别名不接收参数