Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an element exists in the XML using XPath?

Tags:

xml

xpath

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

<Consumers xmlns="http://xml.mycompany.com/XMLSchema">        <Consumer subjectIdentifier="Primary">           <DataSources>                <Credit>                    <CreditReport>                       <AttachedXml><![CDATA[ blah blah]]> 
like image 228
Aravind Yarram Avatar asked Apr 16 '11 22:04

Aravind Yarram


People also ask

How do I check if an element exists in XML?

How to check if xml node exists? To verify if node or tag exists in XML content, you can execute an xpath expression against DOM document for that XML and count the matching nodes. matching nodes > zero – XML tag / attribute exists. matching nodes <= zero – XML tag / attribute does not exist.

What is XPath query in XML?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents.

What is the meaning of '/' in XPath?

Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.


1 Answers

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) 
like image 165
Mads Hansen Avatar answered Sep 22 '22 09:09

Mads Hansen