简体   繁体   中英

Remove empty tags from generated XML after XSLT transformation

I am using XSLT to transform input XML to output XML. My requirement is I need to remove all empty tags from my output XML during XSLT transformation from input.

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. 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). 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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