Trim whitespace from parent element only

前端 未结 3 733
粉色の甜心
粉色の甜心 2020-12-21 12:31

I\'d like to trim the leading whitespace inside p tags in XML, so this:

Hey, italics and italics!

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 13:00

    I would do something like this:

    
        
    
    
    
    
      
         
      
    
    

    This will strip left space from the initial node child of a

    element, if it is a text node. It will not strip space from the first text node child, if it is not the first node child. E.g. in

    Hey there

    I intentionally avoid stripping the space from the front of 'there', because that would make the words run together when rendered in a browser. If you did want to strip that space, change the match pattern to

    match="p/text()[1]"
    

    If you also want to strip trailing whitespace, as your title possibly implies, add these two templates:

    
    
      
         
      
    
    
    
    
       
    
    

    The definitions of the left-trim and right-trim templates are at Trim Template for XSLT (untested). They might be slow for documents with lots of

    s. If you can use XSLT 2.0, you can replace the call-templates with

      
    

    and

      
    

    (Thanks to Priscilla Walmsley.)

提交回复
热议问题