我有像这样的 xml:
<item id="1">
<items>
<item id="2">Text2</item>
<item id="3">Text3</item>
</items>Text1
</item>
如何返回
<item id="1">
的文本('文本1')?<xsl:value-of select="item/text()"/>
什么都不返回。我的 XSLT 是:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="item"/>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
我不知道还要输入什么来提交我的编辑
最佳答案
How to return text of
<item id="1">
('Text1')?<xsl:value-of select="item/text()"/>
returns nothing.
item
element 有多个 text-node 子节点,其中第一个恰好是全空白的——这就是为什么你“什么都没有”。测试节点的字符串值是否不是全空白的一种方法是使用
normalize-space()
功能。在单个 Xpath 表达式中,您需要此 :
/*/text()[normalize-space()][1]
这是一个完整的转换,其结果是所需的文本节点:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:copy-of select="text()[normalize-space()][1]"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<item id="1">
<items>
<item id="2">Text2</item>
<item id="3">Text3</item>
</items>Text1
</item>
产生了想要的正确结果:
Text1
关于xslt - 如何返回没有子节点文本的节点文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134646/