2

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

我想去掉所有的 <s> 和 <p> 标记,以便在 <text> 节点中只有英文句子。

我尝试了以下模板,它确实成功删除了所有标签,但如果那里没有其他字符,它也会删除 <p> 标签之间的空格。例如,我最终会得到:“患者是一位 5​​6 岁的白人男性”

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

有什么想法吗?谢谢!

4

1 回答 1

1

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

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

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

<xsl:strip-space elements="*"/>

</p> <p>在您的样式表中,因为这会去除各种标签对之间的纯空白文本节点。

于 2013-06-25T15:38:34.247 回答