问题描述
我有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>