我有这个(标记)XML 文件,我只想打印其中的“Print this”字符串,忽略后面的内容:
<tag1>
Print this
<tag2>
Do not print this
</tag2>
</tag1>
在我的 XSL 文件中,使用此命令我打印了 tag1
的内容和 tag2
的内容:
<xsl:value-of select="tag1"/>
谢谢!
最佳答案
In my XSL file, with this command I get both the contents of tag1 and the contents of tag2 printed:
<xsl:value-of select="tag1"/>
How can I get just the first, higher level, string?
您的代码生成 tag1
的字符串值元素,根据定义,它是元素的所有文本节点后代的串联。
只生产
the "Print this" string
您需要指定一个只选择相应文本节点的 XPath 表达式:
/tag1/text()[1]
指定 [1]
只需要选择第一个文本节点子节点,否则可能会选择两个文本节点(这仅在 XSLT 2.0 中存在问题,其中 <xsl:value-of>
生成 select
属性中指定的所有节点的字符串值)。
此外,上面的表达式选择了整个文本节点,它的字符串值不是"Print this"
。 .
字符串值实际上是:
"
Print this
"
如果你把 <xsl:value-of>
括起来,就会输出这个在引号中。
准确地生成想要的字符串 "Print this"
使用:
"<xsl:value-of select="normalize-space(/tag1/text()[1])"/>"
关于xml - 我怎样才能得到第一个更高级别的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279109/