When I search for the existence of data in text() of an element using contains, it works for plain data but not when there are carriage returns, new lines/tags in the elemen
Use:
//td[text()[contains(.,'Good bye')]]
Explanation:
The reason for the problem is not that a text node's string value is a multiline string -- the real reason is that the td
element has more than one text-node children.
In the provided expression:
//td[contains(text(),"Good bye")]
the first argument passed to the function contains()
is a node-set of more than one text nodes.
As per XPath 1.0 specification (in XPath 2.0 this simply raises a type error), a the evaluation of a function that expects a string argument but is passed a node-set instead, takes the string value only of the 1st node in the node-set.
In this specific case, the first text node of the passed node-set has string value:
"
Hello world "
so the comparison fails and the wanted td
element isn't selected.
XSLT - based verification:
When this transformation is applied on the provided XML document:
Hello world how are you?
Have a wonderful day.
Good bye!
Hello NJ , how are you?
Have a wonderful day.
the XPath expression is evaluated and the selected nodes (in this case just one) are copied to the output:
Hello world how are you?
Have a wonderful day.
Good bye!