XPath check for non-existing node
Solution 1
Similar to count but maybe more direct depending of what you want is the function boolean
boolean(//Filename)
This returns true if "Filename" node exist and false if not.
Solution 2
You can use the count
function - passing in the path of the nodes you are checking.
If they do not exist, then the value of count
will be 0:
count(//Filename) = 0
Solution 3
Suppose you have the following XML document:
<top>
<function>
<filenamex>c:\a\y\z\myFile.xml</filenamex>
<default>Default.xml</default>
</function>
</top>
then this XPath expression selects either the filename
element when it's present or the default
element when no filename
element is specified:
(/*/function/filename
|
/*/function/default
)
[1]
The shortest way to check if the filename
element exists is:
/*/function/filename
So the first XPath expression could be re-written in the equivalent (but somewhat longer):
/*/function/filename
|
/*/function/default[not(/*/function/filename)]
Solution 4
Given the example Xml from another answer
<top>
<function>
<filenamex>c:\a\y\z\myFile.xml</filenamex>
<default>Default.xml</default>
</function>
</top>
- To get nodes WITH node "filenamex" use
/top/function[filenamex]
- To get nodes WITHOUT node "filenamex" use
/top/function[not(filenamex)]
I felt it necessary to answer here as the other answers did not work as advertised in XmlSpy
Ronny176
Updated on February 08, 2020Comments
-
Ronny176 over 4 years
Im having a bit of trouble finding the right XPath syntax to check if a particular node in my XML exists. I'm only allowed to use XPath (so no XSL or something else like that, it has to be a pure XPath expression syntax).
I have an XML and it has a node
Filename
but it doesn't exist in every case. When the filename isn't specified, my LiveCycle proces will use a different route to fill in the filename. But how do I check if theFilename
node exists? -
intrepidis over 6 yearsSomewhat cryptic intent though, huh?