xpath.js
Xpath pure javascript implementation for node.js
Last updated 8 years ago by yaron .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install xpath.js 
SYNC missed versions from official npm registry.

xpath.js

An xpath module for node, written in pure javascript.

Originally written by Cameron McCormack (blog).

Prepared as a node module by Yaron Naveh (blog, twitter).

Install

Install with npm:

npm install xpath.js

xpath.js is xml engine agnostic but I recommend to use xmldom:

npm install xmldom

Your first xpath:

	var select = require('xpath.js')
	  , dom = require('xmldom').DOMParser

	var xml = "<book><title>Harry Potter</title></book>"
	var doc = new dom().parseFromString(xml)    
	var nodes = select(doc, "//title")
	console.log(nodes[0].localName + ": " + nodes[0].firstChild.data)
	console.log("node: " + nodes[0].toString())

-->

title: Harry Potter
Node: <title>Harry Potter</title>

Get text values directly

    var xml = "<book><title>Harry Potter</title></book>"
    var doc = new dom().parseFromString(xml)    
    var title = select(doc, "//title/text()")[0].data   
    console.log(title)

-->

Harry Potter

Namespaces

	var xml = "<book><title xmlns='myns'>Harry Potter</title></book>"
    var doc = new dom().parseFromString(xml)    
    var node = select(doc, "//*[local-name(.)='title' and namespace-uri(.)='myns/']")[0]
    console.log(node.namespaceURI)

-->

myns

Attributes

    var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"
    var doc = new dom().parseFromString(xml)    
    var author = select(doc, "/book/@author")[0].value    
    console.log(author)

-->

J. K. Rowling

License

MIT

Current Tags

  • 1.1.0                                ...           latest (8 years ago)

9 Versions

  • 1.1.0                                ...           8 years ago
  • 1.0.7                                ...           9 years ago
  • 1.0.6                                ...           11 years ago
  • 1.0.5                                ...           12 years ago
  • 1.0.4                                ...           12 years ago
  • 0.0.4                                ...           12 years ago
  • 0.0.3                                ...           14 years ago
  • 0.0.2                                ...           14 years ago
  • 0.0.1                                ...           14 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 3
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |