How to check if an element exists in the xml using xpath?

后端 未结 6 1169
野性不改
野性不改 2020-12-08 18:47

Below is my element hierarchy. How to check (using xpath) that AttachedXml element is present under CreditReport of Primary

相关标签:
6条回答
  • 2020-12-08 18:59

    Use:

    boolean(/*/*[@subjectIdentifier="Primary"]/*/*/*/*
                               [name()='AttachedXml' 
                              and 
                                namespace-uri()='http://xml.mycompany.com/XMLSchema'
                               ]
           )
    
    0 讨论(0)
  • 2020-12-08 18:59

    The Saxon documentation, though a little unclear, seems to suggest that the JAXP XPath API will return false when evaluating an XPath expression if no matching nodes are found.

    This IBM article mentions a return value of null when no nodes are matched.

    You might need to play around with the return types a bit based on this API, but the basic idea is that you just run a normal XPath and check whether the result is a node / false / null / etc.

    XPathFactory xpathFactory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
    XPath xpath = xpathFactory.newXPath();
    XPathExpression expr = xpath.compile("/Consumers/Consumer/DataSources/Credit/CreditReport/AttachedXml");
    Object result = expr.evaluate(doc, XPathConstants.NODE);
    
    if ( result == null ) {
        // do something
    }
    
    0 讨论(0)
  • 2020-12-08 19:02

    Use the boolean() XPath function

    The boolean function converts its argument to a boolean as follows:

    • a number is true if and only if it is neither positive or negative zero nor NaN

    • a node-set is true if and only if it is non-empty

    • a string is true if and only if its length is non-zero

    • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type

    If there is an AttachedXml in the CreditReport of primary Consumer, then it will return true().

    boolean(/mc:Consumers
              /mc:Consumer[@subjectIdentifier='Primary']
                //mc:CreditReport/mc:AttachedXml)
    
    0 讨论(0)
  • 2020-12-08 19:03

    take look at my example

    <tocheading language="EN"> 
         <subj-group> 
             <subject>Editors Choice</subject> 
             <subject>creative common</subject> 
         </subj-group> 
    </tocheading> 
    

    now how to check if creative common is exist

    tocheading/subj-group/subject/text() = 'creative common'
    

    hope this help you

    0 讨论(0)
  • 2020-12-08 19:06

    Normally when you try to select a node using xpath your xpath-engine will return null or equivalent if the node doesn't exists.

    xpath: "/Consumers/Consumer/DataSources/Credit/CreditReport/AttachedXml"
    

    If your using xsl check out this question for an answer:

    xpath find if node exists

    0 讨论(0)
  • 2020-12-08 19:16

    If boolean() is not available (the tool I'm using does not) one way to achieve it is:

    //SELECT[@id='xpto']/OPTION[not(not(@selected))]
    

    In this case, within the /OPTION, one of the options is the selected one. The "selected" does not have a value... it just exists, while the other OPTION do not have "selected". This achieves the objective.

    0 讨论(0)
提交回复
热议问题