xpath
Advanced tools
Comparing version 0.0.2 to 0.0.4
{ | ||
"name": "xpath", | ||
"version": "0.0.2", | ||
"description": "xpath evaluate and select.", | ||
"version": "0.0.4", | ||
"description": "DOM 3 Xpath implemention and helper for node.js.", | ||
"engines": { | ||
@@ -12,2 +12,5 @@ "node": ">=0.6.0" | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"nodeunit": ">=0.6.4" | ||
}, | ||
"repository": { | ||
@@ -14,0 +17,0 @@ "type": "git", |
@@ -1,7 +0,7 @@ | ||
## xpath.js | ||
An xpath module for node, written in pure javascript. | ||
## xpath | ||
DOM 3 Xpath implemention and helper for node.js. | ||
Originally written by Cameron McCormack ([blog](http://mcc.id.au/xpathjs)). | ||
Prepared as a node module by Yaron Naveh ([blog](http://webservices20.blogspot.com/)). | ||
Thanks to Yaron Naveh ([blog](http://webservices20.blogspot.com/)). | ||
@@ -11,5 +11,5 @@ ## Install | ||
npm install xpath.js | ||
npm install xpath | ||
xpath.js is xml engine agnostic but I recommend to use [xmldom](https://github.com/jindw/xmldom): | ||
xpath is xml engine agnostic but I recommend to use [xmldom](https://github.com/jindw/xmldom): | ||
@@ -21,3 +21,3 @@ npm install xmldom | ||
`````javascript | ||
var select = require('xpath.js') | ||
var xpath = require('xpath') | ||
, dom = require('xmldom').DOMParser | ||
@@ -27,3 +27,3 @@ | ||
var doc = new dom().parseFromString(xml) | ||
var nodes = select(doc, "//title") | ||
var nodes = xpath.select("//title", doc) | ||
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data) | ||
@@ -40,4 +40,4 @@ console.log("node: " + nodes[0].toString()) | ||
var xml = "<book><title>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var title = select(doc, "//title/text()")[0].data | ||
var doc = new dom().parseFromString(xml) | ||
var title = xpath.select("//title/text()", doc).toString() | ||
console.log(title) | ||
@@ -53,3 +53,3 @@ ````` | ||
var doc = new dom().parseFromString(xml) | ||
var node = select(doc, "//*[local-name(.)='title' and namespace-uri(.)='myns/']")[0] | ||
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns/']", doc)[0] | ||
console.log(node.namespaceURI) | ||
@@ -64,8 +64,8 @@ ````` | ||
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 | ||
var doc = new dom().parseFromString(xml) | ||
var author = xpath.select1(doc, "/book/@author").value | ||
console.log(author) | ||
````` | ||
--> | ||
J. K. Rowling |
108
test.js
@@ -1,41 +0,81 @@ | ||
var select = require('./xpath.js') | ||
, dom = require('xmldom').DOMParser | ||
, assert = require('assert') | ||
var xpath = require('./xpath.js') | ||
, dom = require('xmldom').DOMParser | ||
, assert = require('assert') | ||
module.exports = { | ||
'api': function(test) { | ||
assert.ok(xpath.evaluate, 'evaluate api ok.'); | ||
assert.ok(xpath.select, 'select api ok.'); | ||
test.done(); | ||
}, | ||
"get simple xpath": function (test) { | ||
var xml = "<book><title>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var nodes = select(doc, "//title") | ||
assert.equal("title", nodes[0].localName) | ||
assert.equal("Harry Potter", nodes[0].firstChild.data) //first child is the text() node | ||
assert.equal("<title>Harry Potter</title>", nodes[0].toString()) | ||
test.done() | ||
'evaluate': function(test) { | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var nodes = xpath.evaluate('//title', doc, null, xpath.XPathResult.ANY_TYPE, null).nodes; | ||
assert.equal('title', nodes[0].localName); | ||
assert.equal('Harry Potter', nodes[0].firstChild.data); | ||
assert.equal('<title>Harry Potter</title>', nodes[0].toString()); | ||
test.done(); | ||
}, | ||
"get text node": function (test) { | ||
var xml = "<book><title>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var title = select(doc, "//title/text()")[0].data | ||
assert.equal("Harry Potter", title) | ||
test.done() | ||
}, | ||
'select': function(test) { | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var nodes = xpath.select('//title', doc); | ||
assert.equal('title', nodes[0].localName); | ||
assert.equal('Harry Potter', nodes[0].firstChild.data); | ||
assert.equal('<title>Harry Potter</title>', nodes[0].toString()); | ||
test.done(); | ||
}, | ||
"get xpath with namespaces": function (test) { | ||
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
var nodes = select(doc, "//*[local-name(.)='title' and namespace-uri(.)='myns']") | ||
assert.equal("title", nodes[0].localName) | ||
assert.equal("myns", nodes[0].namespaceURI) | ||
test.done() | ||
}, | ||
'select single node': function(test) { | ||
var xml = '<book><title>Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
"get attribute": function (test) { | ||
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 | ||
assert.equal("J. K. Rowling", author) | ||
test.done() | ||
}, | ||
assert.equal('title', xpath.select('//title[1]', doc)[0].localName); | ||
test.done(); | ||
}, | ||
'select text node': function (test) { | ||
var xml = '<book xmlns="test"><title>Harry</title><title>Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
assert.deepEqual('book', xpath.select('local-name(/book)', doc)); | ||
assert.deepEqual('test', xpath.select('namespace-uri(/book)', doc)); | ||
assert.deepEqual('Harry,Potter', xpath.select('//title/text()', doc).toString()); | ||
test.done(); | ||
}, | ||
'select number node': function(test) { | ||
var xml = '<book xmlns="test"><title>Harry</title><title>Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
assert.deepEqual(2, xpath.select('count(//title)', doc)); | ||
test.done(); | ||
}, | ||
'select xpath with namespaces': function (test) { | ||
var xml = '<book><title xmlns="myns">Harry Potter</title></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var nodes = xpath.select('//*[local-name(.)="title" and namespace-uri(.)="myns"]', doc); | ||
assert.equal('title', nodes[0].localName); | ||
assert.equal('myns', nodes[0].namespaceURI) ; | ||
test.done(); | ||
}, | ||
'select attribute': function (test) { | ||
var xml = '<author name="J. K. Rowling"></author>'; | ||
var doc = new dom().parseFromString(xml); | ||
var author = xpath.select1('/author/@name', doc).value; | ||
assert.equal('J. K. Rowling', author); | ||
test.done(); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
128166
3970
1