使用xslt更改所有特殊字符的标签?

时间:2018-07-30 07:56:18

标签: xml xslt

我有这样的xml

<?xml version="1.0" encoding="utf-8"?>
<all>
<Others>1</Others>
<O>(</O>
<B-XREF>1</B-XREF>
<O>)</O>
</authors>
<Others>From</Others></all>

我想将所有包含特殊字符的标签的标签更改为<special>,</special>,替换所有这样的标签。

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match=".">
        <special>
            <xsl:apply-templates select="@*|node()"/>
        </special>
    </xsl:template>

我想这样做,还有其他方法吗?

xml应该像

<?xml version="1.0" encoding="utf-8"?>
<all>
<Others>1</Others>
<authors>
<special>)</special>
<special>,</special> ........

1 个答案:

答案 0 :(得分:1)

您可以使用identity transform模板将输入原样复制到输出,然后将<O>元素修改为<special>

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

<xsl:template match="O">
    <special>
        <xsl:apply-templates />
    </special>      
</xsl:template>
相关问题