How can I trim space in XSLT without replacing repating whitespaces by single ones?

后端 未结 5 518
心在旅途
心在旅途 2020-12-11 01:08

The function normalize-space replaces sequences of whitespaces by a single space and trims the provided string. How can I only trim the string without the repla

5条回答
  •  失恋的感觉
    2020-12-11 01:28

    Using FXSL (open source library for XSLT functional programming, written entirely in XSLT) one simply writes:

    
      
    
      
      
        '
            
        '
      
    
    

    When this transformation is applied on the provided XML document:

    
        To get more information look at:     www.example.com 
    
    

    the wanted, correct result is produced:

    'To get more information look at:     www.example.com'
    

    How does the trim template work?

    It trims the left leading whitespace, then it reverses the resulting string and trims its leading whitespace, then it finally reverses the resulting string.


    II. XPath 2.0 solution:

    Use:

    replace(replace(/*/description, '^\s*(.+?)\s*$', '$1'), '^ .*$', '')
    

    Here is an XSLT - 2.0 - based verification:

    
        
    
     
         ""
     
    
    

    When this transformation is applied on the provided XML document (above), the XPath expression is evaluated and the result of this evaluation is copied to the output:

     "To get more information look at:     www.example.com"
    

提交回复
热议问题