Displaying Date as DD-MM-YYYY within XSLT/XML

前端 未结 4 1459
清酒与你
清酒与你 2021-01-21 19:18

Been trying to format the text to display as DD-MM-YYYY when pulled from XML to XSLT 1.0, since I know it has to be laid out as YYYY-MM-DD within XSD/XML when using xs:date whic

4条回答
  •  星月不相逢
    2021-01-21 19:46

    In XSLT 2.0 you can use tokenize to split the string and then just using xsl:for-each and xsl:sort to reverse it. (I don't have an XSLT2.0 engine right now, but this is pretty close to what you'll need).

    
        
        
    
    

    In XSLT 1.0, you do it using... recursion!

    Here is the crux of it, this takes a date, and then searches for the string before and after the first hyphen (-). Normally, you'd have the substring-before come after the processing of the subtring-after to preserve the order, but here we switch them to ultimately reverse the ouput.

    
        
        
            
                
                    
                
                -
                
            
            
               
            
        
    
    

    Below is the complete template that will reverse the date for you based on your above XML document.

    
    
    
    
      
    
    
    
      
        
        
         
        
      
    
    
    
        
        
            
               
                -
                
                    
                
            
            
               
            
        
    
    
    

    In your code

    You'll need to change this:

    
    

    To this:

    
      
        
      
    
    

提交回复
热议问题