简体   繁体   English

XSLT 转换后从生成的 XML 中删除空标签

[英]Remove empty tags from generated XML after XSLT transformation

I am using XSLT to transform input XML to output XML.我正在使用 XSLT 将输入 XML 转换为输出 XML。 My requirement is I need to remove all empty tags from my output XML during XSLT transformation from input.我的要求是在从输入进行 XSLT 转换期间,我需要从输出 XML 中删除所有空标记。

I tried the instructions given here我尝试了此处给出的说明

https://stackoverflow.com/questions/6648679/removing-empty-tags-from-xml-via-xslt

But probably this talks about the scenario where we are only writing an XSLT for removing the empty tags from an XML.但这可能是关于我们只编写 XSLT 以从 XML 中删除空标签的场景。 In my case, I will have to eliminate the empty tags while transforming from input XML to output XML (In the same XSLT used for transformation).就我而言,我将不得不在从输入 XML 转换为输出 XML 时消除空标记(在用于转换的同一个 XSLT 中)。 Can you please suggest me how to do it?你能建议我怎么做吗?

In XSLT 3 it might suffice to use xsl:where-populated as a "wrapper" for the identity transformation in the match for elements:在 XSLT 3 中,使用xsl:where-populated作为元素匹配中标识转换的“包装器”可能就足够了:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">
    
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="*">
      <xsl:where-populated>
          <xsl:copy>
              <xsl:apply-templates select="@*"/>
              <xsl:apply-templates/>
          </xsl:copy>
      </xsl:where-populated>
  </xsl:template>
  
  <xsl:template match="foo"/>
  
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM