XSLT: Finding last occurence in a string

前端 未结 4 1994
轮回少年
轮回少年 2020-12-18 00:55

Given a form number like:

ABC_12345_Q-10

I want to end up with:

ABC12345

So I need to find the position o

4条回答
  •  悲哀的现实
    2020-12-18 01:02

    @Pavel_Minaev has provided XPath 1.0 amd XPath 2.0 solutions that work if it is known in advance that the number of underscores is 2.

    Here are solutions for the more difficult problem, where the number of undrscores isn't statically known (may be any number):

    XPath 2.0:

    translate(substring($s,
                        1, 
                        index-of(string-to-codepoints($s), 
                                 string-to-codepoints('_')
                                 )[last()] -1
                       ),
              '_',
              ''
             )
    

    XSLT 1.0:

    
     
    
      
      
        
         
        
      
    
      
        
        
    
         
           
           
             
             
           
         
       
    
    

    when this transformation is applied to any XML document (not used), the desired, correct result is produced:

    ABC12345
    

提交回复
热议问题