首页 > 解决方案 > 使用 XSLT 删除双引号

问题描述

如果直接跟在 element 后面,我必须删除双引号<remove>some text</remove>。例如

这要按照“一些植物”之类的东西来解释为“关于动词”

应该

这要按照一些植物等的东西来解释。

<p>This is to be interpreted in "<remove>about verb</remove>" accordance with something "<remove>some of plants</remove>" and so on</p>

应该

<p>This is to be interpreted in <remove>about verb</remove> accordance with something <remove>some of plants</remove> and so on</p>

标签: xmlxslt

解决方案


XSLT 1.0中,这可以通过以下方式处理:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()">
    <xsl:variable name="s" select="preceding-sibling::node()[1][self::remove] and starts-with(., '&quot;')"/>
    <xsl:variable name="e" select="following-sibling::node()[1][self::remove] and substring(., string-length(.))='&quot;'"/>
    <xsl:value-of select="substring(., 1 + $s, string-length(.) -$s -$e)"/>
</xsl:template>

</xsl:stylesheet>

推荐阅读