首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当标记没有值但存在属性时,需要XSLT将其删除

当标记没有值但存在属性时,需要XSLT将其删除
EN

Stack Overflow用户
提问于 2020-09-05 04:12:06
回答 1查看 34关注 0票数 0

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

但是我不能删除具有以下结构的标签:

代码语言:javascript
复制
<cbc:LineExtensionAmount currencyID="EUR"/>

inputXML:

代码语言:javascript
复制
<cbc:LineTotal currencyID="EUR">1989.65</cbc:LineTotal>
<cbc:LineAmount currencyID="EUR"/>
<cbc:dummy/>

预期输出:

代码语言:javascript
复制
<cbc:LineTotal currencyID="EUR">1989.65</cbc:LineTotal>

我目前的XSLT如下所示:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2020-09-05 04:21:43

这对你有用吗:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63747497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档