Removing empty tags from XML via XSLT

后端 未结 6 620
独厮守ぢ
独厮守ぢ 2020-12-09 10:42

I had an xml of the following pattern


    
      Ahmed
             


        
相关标签:
6条回答
  • 2020-12-09 11:21

    Your question is underspecified. What does empty mean? Is <outer> empty here?

    <outer><inner/></outer>
    

    Anyway, here's one approach that might fit your bill:

    <xsl:template match="*[not(.//@*) and not( normalize-space() )]" priority="3"/>
    

    Note you might have to tweak the priority to fit your needs.

    0 讨论(0)
  • 2020-12-09 11:22

    This transformation doesn't need any conditional XSLT instructions at all and uses no explicit priorities:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <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:stylesheet>
    

    When applied on the provided XML document:

    <Person>
        <FirstName>Ahmed</FirstName>
        <MiddleName/>
        <LastName>Aboulnaga</LastName>
        <CompanyInfo>
            <CompanyName>IPN Web</CompanyName>
            <Title/>
            <Role></Role>
            <Department>
            </Department>
        </CompanyInfo>
    </Person>
    

    it produces the wanted, correct result:

    <Person>
       <FirstName>Ahmed</FirstName>
       <LastName>Aboulnaga</LastName>
       <CompanyInfo>
          <CompanyName>IPN Web</CompanyName>
       </CompanyInfo>
    </Person>
    
    0 讨论(0)
  • 2020-12-09 11:26

    (..) Is there any fix for this?

    The tag on two lines is not an empty tag. It is a tag containing spaces inside (like new lines and possibly some kind of white space characters). The XPath 1.0 function normalize-space() allows you to normalize the content of your tags by stripping unwanted new lines.

    Once you have applied the function to the tag content you can then check for the empty string. A good way to do this is by applying the XPath 1.0 boolean() function to the tag content. If the content is a zero-length string its result will be false.

    Finally you can embed everything slightly changing your identity transform. You do not need xsl:if instructions or any other additional template.

    The final transform will look like this:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
                 <xsl:apply-templates 
                      select="node()[boolean(normalize-space())]
                             |@*"/>
         </xsl:copy>
     </xsl:template>
    
    </xsl:stylesheet>
    

    Additional note

    Your xsl:if instruction is currently checking also for empty attributes. In that way you are actually removing also non-empy tags with empty attributes. It does not sound like just "Removing empty tags". So be careful, or you question is missing some detail, or you are using unsafe code.

    0 讨论(0)
  • 2020-12-09 11:29

    From what I have found on the net, this is the most correct answer:

    <?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"/>
        <xsl:template match="/">
            <xsl:apply-templates select="*"/>
        </xsl:template>
        <xsl:template match="*">
                <xsl:if test=".!=''">
                    <xsl:copy>
                      <xsl:copy-of select="@*"/>
                      <xsl:apply-templates/>
                    </xsl:copy>
                </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    
    0 讨论(0)
  • 2020-12-09 11:34

    You can use the following xslt to remove empty tags/attributes:

    <?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>
    
    0 讨论(0)
  • 2020-12-09 11:39
    <xsl:template match="@*|node()">
      <xsl:if test="normalize-space(.) != '' or ./@* != ''">
        <xsl:copy>
           <xsl:copy-of select = "@*"/>
           <xsl:apply-templates/>
        </xsl:copy>
      </xsl:if>
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题