tags:

views:

42

answers:

2

Hi,

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 the "filename" node exists?

thanks for your response!

Ronald Kamp

Junior LiveCycle/Flex developer

A: 

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
Oded
A: 

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)]
Dimitre Novatchev