Dinesh
Dinesh

Reputation: 33

Need explanation of XPath syntax

Can anyone explain the XPath syntax written below:

xmlDoc.documentElement.selectNodes("//*/Expression[.!='']")

Basically, I need clarification of the argument path of the above syntax.

Upvotes: 2

Views: 132

Answers (3)

kjhughes
kjhughes

Reputation: 111491

//* selects all elements.

//*/Expression selects all Expression child elements.

//*/Expression[.!=''] selects all Expression child elements with non-empty string-values.

XML Path Language (XPath):

The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.

Note: This is not the same as being empty. Empty elements have no children at all.

Consider an example:

<Expression id="e1"> 
  <Expression id="e2"/>  
  <Expression id="e3"><c/></Expression> 
  <Expression id="e4"> 
    <b/> 
  </Expression>  
  <Expression id="e5"> 
    <a>text1</a> 
  </Expression>  
  <Expression id="e6">test2<c/></Expression> 
</Expression>

Expression e1 would not be selected because as the root element it is not a child of any other element.

Expression e2 would not be selected because its string-value is ''.

Expression e3 would not be selected because its string-value is '', even though e3 is not itself empty.

Expression elements e4, e5, and e6 would be selected because they all have non-empty string-values.

Upvotes: 2

har07
har07

Reputation: 89285

//*

Select any element anywhere in the XML document

/Expression[.!='']

Then from that element, get all direct child <Expression> elements having non-empty value.

Upvotes: 0

S. Pauk
S. Pauk

Reputation: 5318

Select all the <Expression> elements which:

  1. are not root elements
  2. and have non-empty value

//*/ - this means any but root

Expression - element name

[.!=''] - this means current node value is not empty, i.e. current node . is not != empty ''

Upvotes: 4

Related Questions