Converting XML to escaped text in XSLT

前端 未结 8 1676
挽巷
挽巷 2020-12-03 02:54

How can I convert the following XML to an escaped text using XSLT?

Source:



  

        
8条回答
  •  清歌不尽
    2020-12-03 03:19

    Your code works the way it does because xsl:value-of retrieves the string-value of the node set.

    To do what you want, I'm afraid that you'll have to code it explicitly:

        
            
                
            
        
    
        
            
            <
            
    
            
            
                 xmlns
                
                    :
                    
                
                ='
                
                    
                
                '
            
    
            
            
                 
                
                ='
                
                    
                
                '
            
    
            
            >
    
            
            
    
            
            </
            
            >
        
    
        
            
                
            
        
    
        
            <?
            
             
            
                
            
            ?>
        
    
        
            
            
                
                
                
                    &amp;
                    &lt;
                    &gt;
                    &quot;
                    &apos;
                    
                
                
                    
                
            
        
    

    Note that this solution ignores comment nodes, and inserts unneccessary namespace nodes (as namespace:: axis will include all nodes inherited from parent). Regarding namespaces, however, the resulting quoted XML will be semantically equivalent to the example that you provided in your reply (since those repeated redeclarations don't really change anything).

    Also, this won't escape the declaration, simply because it is not present in XPath 1.0 data model (it's not a processing instruction). If you actually need it in the output, you'll have to insert it manually (and make sure that encoding it specifies is consistent with serialization encoding of your XSLT processor).

提交回复
热议问题