XSLT multiple string replacement with recursion

前端 未结 5 1559
花落未央
花落未央 2021-01-16 13:38

I have been attempting to perform multiple (different) string replacement with recursion and I have hit a roadblock. I have sucessfully gotten the first replacement to work

5条回答
  •  被撕碎了的回忆
    2021-01-16 14:13

    Although this question was asked (and answered) several years ago, neither this answer nor the (many!) other variants I found while searching the 'net over the last couple of days were able to do what I needed: replace multiple strings in nodes which may contain several kb of text.

    Dimitre's version works well when nodes contain very little text, but when I tried to use it I almost immediately fell foul of the dreaded stack overflow (recursive calls, remember!) The problem with Dimitre's solution is that it tries to match the search patterns to the beginning of the text. This means that many (recursive) calls are made, each call using the right-most n-1 characters of the original text. For a 1k text that means over 1000 recursive calls!

    After digging around for alternatives I came across an example by Ibrahim Naji (http://thinknook.com/xslt-replace-multiple-strings-2010-09-07/) which uses the more conventional substring-before/substring-after combination to perform the replacement. However, that code is limited to a single replacement string for any number of search strings.

    I decided, therefore, that it was time to actually get my hands dirty (and learn XSLT at the same time!) The result is the following code which performs multiple string replacements (specified via an internal template, but that could easily be replaced with an external file, for example), and which (so far in my tests) doesn't suffer from excessive recursive calls.

    It should be noted that the replacements are very basic (as are most other existing implementations) meaning that no attempts are made to only match entire words, for example. I hope the comments are enough to explain the way it works, particularly for other XSLT beginners (like myself).

    And now the code...

    
    
    
        
    
        
    
        
        
            
                
            
        
    
        
        
            
                
            
        
    
        
        
            
                
                    <i>
                    <em>
                
                
                    </i>
                    </em>
                
                
                    <b>
                    <strong>
                
                
                    </b>
                    </strong>
                
            
        
    
        
        
    
        
        
            
            
            
            
            
    
                
                 
                    
                
    
                
                
                    
                
    
                
                
                    
                    
                        
                        
                    
                
    
                
                
                    
                    
                    
                        
                        
                            
                                
                                
                            
                        
    
                        
                        
                            
                        
                    
                
            
        
    
    

提交回复
热议问题