How to preserve Empty XML Tags after XSLT - prevent collapsing them from to

后端 未结 10 2624
不思量自难忘°
不思量自难忘° 2020-12-18 22:31

Say I have a very simple XML with an empty tag \'B\':


  foo
  
  bar

         


        
10条回答
  •  爱一瞬间的悲伤
    2020-12-18 23:00

    It's 7 years late, but for future readers I will buck the trend here and propose an actual solution to the original question. A solution that does not modify the original with spaces or the output directive.

    The idea was to use an empty variable to trick the parser.

    If you only want to do it just for one tag B, my first thought was to use something like this to attach a dummy variable.

    
    
        
      
        
        
          
    
    

    But I found that in fact, even the dummy variable is not necessary. This preserved empty tags, at least when tested with xsltproc in linux :

    
      
        
        
          
    
    

    For a more generic solution to handle ALL empty tags, try this:

      
    
      
        
            
            
        
      
    

    Again, depending on how smart your parser is, you may not even need the dummy variable.

提交回复
热议问题