XSLT:在某些标签内剥离标签,但保留外部标签

时间:2019-03-11 15:13:00

标签: xml xslt

我有以下已钝化的xml文件,我需要在TXT中删除标签。我创建了一个样式表,可以成功剥离文件中的所有标签,但是我只希望它仅剥离TXT块内的 标签。要实现此目标,我需要对XSLT进行哪些更改?

XML

<DOC>
    <ID>1234</ID>
<TXT>
<A><DESC type="PERSON">George Washington</DESC> lived in a house called <DESC type="PLACE">Mount Vernon.</DESC></A>
<A><DESC type="PERSON">Thomas Jefferson</DESC> lived in a house called <DESC type="PLACE">Monticello.</DESC></A>
</TXT>
</DOC>

XSLT

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template name="strip-tags">
        <xsl:param name="TXT"/>
        <xsl:choose>
            <xsl:when test="contains($TXT, 'A')">
                <xsl:value-of select="$TXT"/>
                <xsl:call-template name="strip-tags">
                    <xsl:with-param name="TXT" select="substring-after($TXT, 'A')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$TXT"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

当前输出

<?xml version="1.0" encoding="UTF-8"?>
    1234

    George Washington lived in a house called Mount Vernon.
    Thomas Jefferson lived in a house called Monticello.

所需的输出

<?xml version="1.0" encoding="UTF-8"?>
    <DOC><ID>1234</ID>

    <TXT>George Washington lived in a house called Mount Vernon.
    Thomas Jefferson lived in a house called Monticello.</TXT>
    </DOC>

1 个答案:

答案 0 :(得分:1)

重新制定您的要求:

  

除那些元素后代外,每个节点都转换为自身   TXT元素

使用identity transformation

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="TXT//*">
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>

结果:

<DOC>
    <ID>1234</ID>
   <TXT>
George Washington lived in a house called Mount Vernon.
Thomas Jefferson lived in a house called Monticello.
</TXT>
</DOC>