xpath
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The xpath
npm package provides a DOM 3 XPath 1.0 implementation along with helper functions for use with JavaScript, extending support to node.js as well. At its core, it allows for efficient querying and navigation of XML documents.
To use xpath
, you first need to install the package via npm using npm install xpath
. An xml engine is required in conjunction with xpath
, for which the authors recommend xmldom
and it can be installed using npm install @xmldom/xmldom
.
You can import the necessary modules in your code with require statements: var xpath = require('xpath');
and var dom = require('@xmldom/xmldom').DOMParser;
. Then you can use the xpath.select()
function to run XPath queries on your XML documents.
Here's an example usage:
var xpath = require('xpath');
var dom = require('@xmldom/xmldom').DOMParser;
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var nodes = xpath.select("//title", doc);
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data);
console.log("Node: " + nodes[0].toString());
In this example, xpath.select("//title", doc)
is used to select all <title>
elements from the XML document.
You can also evaluate string values directly, interact with namespaces, easily map namespaces, and interact with attributes.
The xpath
package offers comprehensive API documentation which is hosted on GitHub. It provides in-depth information about the different methods and functions provided by the xpath
package and their usage.
You can access the xpath
API Documentation at this URL: https://github.com/goto100/xpath/blob/master/docs/xpath methods.md.