使用XSLT,我试图剥离特定节点中的所有标签,同时保留这些标签之间的空格。
我得到的XML如下:
<text>
<s id="s2"> The patient is a <p id="p22">56-year-old</p> <p id="p28">Caucasian</p> <p id="p30">male</p></s></text>我想去掉所有的和标签,以便在节点中只包含英语句子。
我尝试了下面的模板,它确实成功地删除了所有标签,但如果标签之间没有其他字符,它也会删除标签之间的空格。例如,我最后会说:“病人是一个56岁的白人男性。”
<xsl:template name="strip-tags">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="strip-tags">
<xsl:with-param name="text" select="substring-after($text, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>有什么想法吗?谢谢!
发布于 2013-06-25 23:38:34
保留空白但删除标签的文本内容正是元素节点的“字符串值”的定义。所以你可以简单地使用
<xsl:value-of select="$text" />(假设$text包含<text>元素节点)。这也假设你没有
<xsl:strip-space elements="*"/>在样式表中,这将剥离不同的</p> <p>标记对之间的纯空格文本节点。
https://stackoverflow.com/questions/17301451
复制相似问题