Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get node value / innerHTML with XPath?

I have a XPath to select to a class I want: //div[@class='myclass']. But it returns me the whole div (with the <div class='myclass'> also, but I would like to return only the contents of this tag without the tag itself. How can I do it?

like image 985
Tom Smykowski Avatar asked Jun 05 '12 13:06

Tom Smykowski


People also ask

Can we get element by XPath in Javascript?

Use the document. evaluate() method to get an element by XPath. The method returns an XPathResult based on the provided XPath expression and the supplied parameters.

What is current node in XPath?

Current node is the node that the XPath processor is looking at when it begins evaluation of a query. In other words, the current node is the first context node that the XPath processor uses when it starts to execute the query. During evaluation of a query, the current node does not change.


2 Answers

node() = innerXml  text() = innerText 

both are arrays, so text()[1] is a first children text node...

like image 164
Nikola Bogdanović Avatar answered Oct 02 '22 03:10

Nikola Bogdanović


With xpath, the thing you will get returned is the last thing in the path that is not a condition. What that means? Well, conditions are the stuff between []'s (but you already knew that) and yours reads like pathElement[that has a 'class' attribute with value 'my class']. The pathElement comes directly before the [.

All the stuff outside of []'s then is the path, so in //a/b/c[@blah='bleh']/d a, b, c and d are all path elements, blah is an attribute and bleh a literal value. If this path matches it will return you a d, the last non-condition thing.

Your particular path returns a (series of) div, being the last thing in your xpath's path. This return value thus includes the top-level node(s), div in your case, and underneath it (them) all its (their) children. Nodes can be elements or text (or comments, processing instructions, ...).

Underneath a node there can be multiple text nodes, hence the array pOcHa talks about. x/text() returns all text that is a direct child of x, x/node() returns all child nodes, including text.

like image 43
jos Avatar answered Oct 02 '22 03:10

jos