XSLT:剥离标记,保留空格

时间:2013-06-25 15:31:19

标签: xml xslt-1.0

使用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>

我想删除所有&lt; s&gt;和&lt; p&gt;标签,以便我在&lt; text&gt;中只有英文句子节点

我尝试过以下模板,该模板成功删除了所有标记,但它也删除了&lt; p&gt;之间的空格。标签,如果没有其他字符。例如,我最终会说:“病人是一名56岁的女士”

<xsl:template name="strip-tags">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&lt;')">
            <xsl:value-of select="substring-before($text, '&lt;')"/>
            <xsl:call-template name="strip-tags">
                <xsl:with-param name="text" select="substring-after($text, '&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

有什么想法?谢谢!

1 个答案:

答案 0 :(得分:1)

保留了空格但删除了标签的文本内容正是元素节点的“字符串值”的定义。所以你可以简单地使用

<xsl:value-of select="$text" />

(假设$text包含<text>元素节点)。这也假设你

<xsl:strip-space elements="*"/>
样式表中的

,因为它会在各对</p> <p>标记之间删除仅空白文本节点。

相关问题