当标签没有值但属性存在时,需要XSLT删除标签

问题描述

我有XSLT,如果其值为空/空,则可以很好地删除标记。但是我无法删除具有以下结构的标签:

<cbc:LineExtensionAmount currencyID="EUR"/>

inputXML:

<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>

任何建议/指针都很好。

解决方法

这项工作对您有用吗

<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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...