Removing empty tags from XML via XSLT

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

I had an xml of the following pattern


    
      Ahmed
             


        
6条回答
  •  我在风中等你
    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:

    
     
     
    
     
         
                 
         
     
    
    
    

    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.

提交回复
热议问题