【问题标题】:How to remove empty xml elements except few specific empty tags如何删除除少数特定空标签外的空 xml 元素
【发布时间】:2020-07-24 04:48:00
【问题描述】:

我有带有数据的 xml。我需要删除所有没有值的空标签。但我不必删除 2 个特定的 xml 标签,即使标签是空值。

 <Executive>
 <title></title>
<executivename></executivename>
</Executive> 

我的 xml 很大,有很多元素,但上面的 2 个元素(标题和执行名称)即使它们有空值,我也不必删除。下面是我用来删除所有空值标签的 xslt 代码。但我不知道如何排除上述两个元素。有人可以帮我解决这个问题吗?

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
<xsl:template match=
    "*[not(@*|*|comment()|processing-instruction()) 
     and normalize-space()='' 
      ]"/>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    经过这么多的线索和错误,我找到了方法。我添加了 not(self::title) 和 not(self::executivename) 如下,从空标签删除逻辑中排除了这两个元素。

    <xsl:template match=
        "*[not(@*|*|comment()|processing-instruction()) and not(self::title) and not(self::executiveName) 
         and normalize-space()='' 
          ]"/>
    

    如果有人提出更优化的解决方案,那将是很大的帮助。

    谢谢!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      相关资源
      最近更新 更多