xml xslt:与元素关联的文本值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3469371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:15:15  来源:igfitidea点击:

xslt: value of text associated with element

xmlxslt

提问by Jason S

If I have an XML file that includes

如果我有一个 XML 文件,其中包括

<param name="foo" value="5000" >foo is a way of making pasta sauce</param>
<param name="bar" value="3000" >bar is controlling the beer taps</param>

and I want to use XSLT to process this into an HTML file, with the name and value attributes and the text as a description, how can I get the XML node text?

我想用 XSLT 把它处理成一个 HTML 文件,用 name 和 value 属性和文本作为描述,我怎样才能得到 XML 节点文本?

<xsl:for-each select="param">
   <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@value"/></td>
      <td><xsl:text> </xsl:text></td>
   </tr>
</xsl:for-each>

The above XSLT fragment does successfully get the name and value attributes, but it fails to get the text, and I think I'm missing something obvious but I don't know what.

上面的 XSLT 片段确实成功获取了 name 和 value 属性,但它无法获取文本,我想我遗漏了一些明显的东西,但我不知道是什么。

回答by YoK

Try this

尝试这个

<xsl:for-each select="param">
   <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@value"/></td>
      <td><xsl:value-of select="text()"/></td>
   </tr>
</xsl:for-each>

回答by Jason S

aha, this also seems to work:

啊哈,这似乎也有效:

<xsl:for-each select="param">
   <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@value"/></td>
      <td><xsl:value-of select="."/></td>
   </tr>
</xsl:for-each>