is there any operation such as trim in xslt?

前端 未结 4 1963
再見小時候
再見小時候 2020-12-17 19:31

i wrote a xslt code which converts a xml file to a html file which contains lot of tables, one of the column contains messages(very long messages), but that line starts with

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 20:09

    is there any operation such as trim in xslt?

    I. XSLT 1.0

    No, and it is rather difficult to perform "trim" in XSLT 1.0.

    Here is the trim function/template from FXSL:

    
    
      
    
      
    
      
      
        '
            
        '
      
    
    

    When this transformation is performed (you have to download at least a few other stylesheet modules, which comprise the complete import tree) on this XML document:

    
    
       This is    some text   
    
    
    

    the wanted, correct result is produced:

    'This is    some text'
    

    II In XSLT 2.0 / XPath 2.0

    Still a little bit tricky, but very short:

         if (string(.))
           then replace(., '^\s*(.+?)\s*$', '$1')
           else ()
    

    Here is the complete, corresponding transformation:

    
        
    
     
         ""
     
    
    

    and when applied on the same XML document (above), the same correct result is produced:

    "This is    some text"
    

提交回复
热议问题