Converting XML to escaped text in XSLT

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

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

Source:



  

        
8条回答
  •  一生所求
    2020-12-03 03:25

    I attempted to implement the answer provided by Pavel Minaev and want to point out that this is very dangerous for large strings as each character in the input string is recursed over individually, causing the recursion depth to quickly run out. I attempted to run it over a few lines of text and it caused a stack overflow (lol).

    Instead, I use a template that does not need to examine each individual char, rather it will out put the text until it finds a string that needs to be replaced. This can then be used to escape characters:

    
        
        
          
        
            
                
                        
                
                    
                    
                    
                
            
            
                
            
           
     
    

    Then its just a matter of calling that template for the char that you want to escape..

    
                
                
                
        
    

    In order to escape multiple characters in the one string, I used a wrapper template that uses variables...

    
        
    
        
        
                
                
                
                        
        
    
             
            
                
                
                
            
        
    
             
            
                
                '
                
            
                 
    
             
            
                
                
                
            
        
    
        
            
                
                
                
            
             
        
             
     
    

    This proved to be much safer for large strings as it no longer has to recurse for each individual character in the input string.

提交回复
热议问题