How to tell using XPath if an element is present and non empty?

Maybe I'm a bit late here, but answers are a bit confusing. This one will always returns false when text is blank or with spaces but no chars.

boolean//Holding/Policy/PolNumber/child/text()[normalize-space()]

How about expression="#[?xpath('//acord:Holding/acord:Policy/acord:PolNumber').text != empty]" ? This should work in in all situations


You can use boolean(...) for checking if it's empty, but make sure to look inside the element.

boolean(//PolNumber/node())

This also works if other nodes are contained. If you want to limit to text nodes, replace node() by text(). You could want to use //text() instead, then the query will also yield true for text nodes inside other child elements of <PolNumber/>.


Use:

boolean(//acord:Holding/acord:Policy/acord:PolNumber/text()[1])

this produces true() if //acord:Holding/acord:Policy/acord:PolNumber has a first text-node child, and false() otherwise.

Do note: This is more efficient than counting all text-node children just to compare the count with 0.

Tags:

Xml

Xpath

Mule