
“result” is used if you have an existing XPathResult you’d like to reuse.“resultType” lets you describe the format of of the XPathResult that is returned.For HTML documents, it’s best to pass “null” “namespaceResolver” is used to resolve namespaces within the XPath.Normally this is “document” (the starting location of all HTML documents), but it can be any node in the document. “contextNode” is the starting point of the search.“xpathExpression” is the query string to point to the nodes you wish to locate.JavaScript does this through the following statement: let xpathResult = document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result ) For our purposes, we’re going to show what it will take to extract those nodes that contain certain text.Īs mentioned earlier, XPath is a query language, and as such we’ll use that language to contruct a query string that will tell JavaScript to access a certain node based upon that query string. For that, I would suggest looking at the Mozilla XPath documentation. This article will not go into all the details of using XPath. But XPath can see these nodes, and thus we’ll use XPath to get to those records. Selectors can’t see these textNodes, which is why we couldn’t target all the “Horse” records directly earlier. For example, suppose you have this HTML structure: My cat's name is Fluffy.

Remember, an HTML document is a type of XML document, so it’s logical that an XML query language can be used to traverse an HTML document.īy why use XPath when methods like querySelector and querySelectorAll work great? It’s all because of the structure of an HTML document. Put simply, XPath is a query language for locating nodes in an XML document.
XPATH GET PLAIN TEXT HOW TO
You end up with something like this: // assumes the table id is "animalTable" const animalTable = document.getElementById("animalTable") const records = const horseRecords = records.filter( record => = "Horse" ) īut did you know there is way to directly find the text within a ? It’s called XPath, and here’s how to use it. The problem is you cannot use querySelectorAll to directly access the “Type” of animal.

Normally, a quick querySelectorAll will get all the rows, but there’s a catch: A typical row looks like this: Cat Fluffy 0.24 3.99 48.28 Your job is to get a collection of rows within the and return all the ones in which the “Type” is “Horse”. In our example, there is a listing of animals, and the first column in the table contains the “Type” of animal (Cat, Dog, Horse, etc). Suppose you have an HTML table in your application that you need to extract data from the text within the element.

(Reposted from my blog posting on July 22nd, 2020)
