Check if a string is null or empty in XSLT

前端 未结 14 1164
梦毁少年i
梦毁少年i 2020-11-27 09:12

How can I check if a value is null or empty with XSL?

For example, if categoryName is empty? I\'m using a when choosing construct.

For

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 09:47

    How can I check if a value is null or empty with XSL?

    For example, if categoryName is empty?

    This is probably the simplest XPath expression (the one in accepted answer provides a test for the opposite, and would be longer, if negated):

    not(string(categoryName))
    

    Explanation:

    The argument to the not() function above is false() exactly when there is no categoryName child ("null") of the context item, or the (single such) categoryName child has string value -- the empty string.

    I'm using a when choosing construct.

    For example:

    
        
            
        
        
            
        
    
    

    In XSLT 2.0 use:

    
    

    Here is a complete example:

    
         
    
     
    
     
      
     
    
    

    When this transformation is applied on the following XML document:

    X
    

    the wanted, correct result is produced:

    X
    

    When applied on this XML document:

    
    

    or on this:

    
    

    or on this

    Y
    

    the correct result is produced:

    Other
    

    Similarly, use this XSLT 1.0 transformation:

    
     
    
     
    
      
        
      
    
    

    Do note: No conditionals are used at all. Learn more about the importance of avoiding conditional constructs in this nice Pluralsight course:

    "Tactical Design Patterns in .NET: Control Flow"

提交回复
热议问题