Given the desire to use XPath to select an XML Text Node based on its value, is there a better way than:
//*[text()="foo"]/text()
I’m assuming that there’s something like:
//text()[self="foo"]
…but I don’t know what to put for self.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have generally found the right answer:
However, please note the following:
The XPath
//pseudo-operator should be avoided as much as possible whenever the structure of the XML document is statically known, because its evaluation causes complete subtrees (in the worst case the complete document tree) to be traversed and is very inefficient. If you know that all text nodes are children ofcand allcin the document can be selected by/a/b/c, then use:/a/b/c/text(). If you only know that all text nodes are descendants ofc, then it is still much better to use:/a/b/c//text()than//text()or even than//c//text()Quite often the text nodes contain whitespace that we tend not to notice. In such cases one needs to write:
rather than