XSLT multiple string replacement with recursion

前端 未结 5 1564
花落未央
花落未央 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:08

    This transformation is fully parameterized and doesn't need any tricks with default namespaces:

    
     
     
    
     
      
       
    
       
    quick slow fox elephant brown white

    when it is applied on this XML document:

    The quick
    brown fox
    

    the wanted, correct result is produced:

    The slow
    white elephant

    Explanation:

    The text is scanned from left to right and at any position, if the remaining string starts with one of the specified patterns, then the starting substring is replaced by the replacement specified for the firat matching patterns.

    Do note: If we have search patterns:

       "relation"   --> "mapping" 
       "corelation" --> "similarity"
    

    in the above order, and text:

       "corelation"
    

    then this solution produces the more correct result:

    "similarity"
    

    and the currently accepted solution by @Alejandro) produces:

    "comapping"
    

    Edit: With a small update we get another improvement: If at a given location more than one replace is possible, we perform the longest replace.

    
        
        
    
        
            
                
    
                
    quick slow fox elephant brown white

    Thus, if we have two reps such as "core" --> "kernel" and "corelation" --> "similarity", The second would be used for a text containing the word "corelation", regardless of how the reps are ordered.

提交回复
热议问题