【问题标题】:XSLT: Strip tags within certain tag, but preserve outer tagXSLT:去除特定标签内的标签,但保留外部标签
【发布时间】:2019-03-11 15:13:00
【问题描述】:

我有以下虚拟 xml 文件,我需要去除 TXT 中的标签。我已经创建了一个样式表,它成功地去除了文件中的 all 标记,但我只希望它只去除 TXT 块中的标记 within。我需要对 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>

【问题讨论】:

    标签: xml xslt


    【解决方案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>
    

    【讨论】:

    • 太棒了,这正是我所需要的!非常感谢!
    猜你喜欢
    • 2015-12-13
    • 2019-09-23
    • 2013-03-24
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多