【问题标题】:Need XSLT to remove the tag when it has no value but attribute exists当标签没有值但属性存在时,需要 XSLT 删除标签
【发布时间】:2020-12-24 02:24:03
【问题描述】:

我有 XSLT,如果它们的值为空/null,它可以很好地删除标签。

但我无法删除具有以下结构的标签:

<cbc:LineExtensionAmount currencyID="EUR"/>

输入XML:

<cbc:LineTotal currencyID="EUR">1989.65</cbc:LineTotal>
<cbc:LineAmount currencyID="EUR"/>
<cbc:dummy/>

预期输出:

<cbc:LineTotal currencyID="EUR">1989.65</cbc:LineTotal>

我目前的 XSLT 如下:

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

<xsl:template match="node()">
    <xsl:if test="normalize-space(string(.)) != ''
                    or count(@*[normalize-space(string(.)) != '']) > 0
                    or count(descendant::*[normalize-space(string(.)) != '']) > 0
                    or count(descendant::*/@*[normalize-space(string(.)) != '']) > 0">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
    </xsl:if>
</xsl:template>

<xsl:template match="@*">
    <xsl:if test="normalize-space(string(.)) != ''">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
        </xsl:copy>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt xml-parsing


    【解决方案1】:

    这对你有用吗:

    <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>
    
    <!-- remove elements with no content other than attributes -->
    <xsl:template match="*[not(node())]"/>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 好。如果您的问题得到解答,请通过接受答案来关闭它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多