【发布时间】:2021-12-31 02:12:05
【问题描述】:
假设我有以下 XML:
<schools>
<school>
<description>Helpful description</description>
</school>
<school>
<description>Another <important>really important</important> description</description>
</school>
<school>
<description><important>WARNING</important> this is stupid</description>
</school>
</schools>
我想将整个描述元素作为文本获取
Helpful description
Another really important description
WARNING this is stupid
我能够在<important/> 第一次出现之前获得text(),但之后不行。我尝试过类似的方法。
<xsl:template match="description" mode="important_recursive">
<xsl:value-of select="node()/preceding-sibling::text()"/>
<xsl:apply-templates select="important" mode="important_recursive"/>
</xsl:template>
<xsl:template match="important" mode="important_recursive">
<span><xsl:value-of select="text()"/></span>
</xsl:template>
【问题讨论】:
-
您说“作为文本”,但您的代码显示
span。请显示您想要获得的准确、完整的结果(作为代码)。 -
内置模板也会复制文本节点,因此不清楚您何时决定需要采取任何复杂的努力来获取文本内容,它应该在不编写模板的情况下获得输出。