XSL / XPath expression to check if a node contains at least one non-empty child

前端 未结 4 572
小蘑菇
小蘑菇 2020-12-14 06:52

I need to check if an XML node has at least one non-empty child. Applied to this XML the expression should return true


    

        
4条回答
  •  無奈伤痛
    2020-12-14 07:30

    Here's one XPath that should accomplish the job:

    count(/*/node/*[text()]) > 0
    

    When used in a sample XSLT:

    
    
      
      
    
      
         
      
    
    
    

    ...which is, in turn, applied to the provided example XML:

    
      
        
        
        value
      
    
    

    ...the expected result is produced:

    true
    

    If we apply the same XSLT against a simply modified XML:

    
      
        
        
        
      
    
    

    ...again, the expected result is produced:

    false
    

    Explanation:

    The XPath used searches for all children of a element (which are, in turn, children of the root element) that have a non-empty text value (as specified by text()); should the count of such children be greater than 0, then the XPath resolves to true.

提交回复
热议问题