xpath
Advanced tools
Comparing version 0.0.5 to 0.0.6
{ | ||
"name": "xpath", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "DOM 3 Xpath implemention and helper for node.js.", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -47,3 +47,3 @@ ## xpath | ||
`````javascript | ||
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>" | ||
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>" | ||
var doc = new dom().parseFromString(xml) | ||
@@ -56,3 +56,13 @@ var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns/']", doc)[0] | ||
myns | ||
## Namespaces with easy mappings | ||
`````javascript | ||
var xml = "<book xmlns:bookml='http://example.com/book'><bookml:title>Harry Potter</bookml:title></book>" | ||
var select = xpath.useNamespaces({"bookml": "http://example.com/book"}); | ||
console.log(select('//bookml:title/text()', doc)[0].nodeValue)); | ||
````` | ||
--> | ||
Harry Potter | ||
## Attributes | ||
@@ -62,3 +72,3 @@ `````javascript | ||
var doc = new dom().parseFromString(xml) | ||
var author = xpath.select1(doc, "/book/@author").value | ||
var author = xpath.select1("/book/@author", doc).value | ||
console.log(author) | ||
@@ -65,0 +75,0 @@ ````` |
34
test.js
var xpath = require('./xpath.js') | ||
, dom = require('xmldom').DOMParser | ||
, assert = require('assert') | ||
, assert = require('assert'); | ||
@@ -72,2 +72,34 @@ module.exports = { | ||
'select xpath with namespaces, using a resolver': function (test) { | ||
var xml = '<book xmlns:testns="http://example.com/test"><testns:title>Harry Potter</testns:title><testns:field testns:type="author">JKR</testns:field></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var resolver = { | ||
mappings: { | ||
'testns': 'http://example.com/test' | ||
}, | ||
lookupNamespaceURI: function(prefix) { | ||
return this.mappings[prefix]; | ||
} | ||
} | ||
var nodes = xpath.selectWithResolver('//testns:title/text()', doc, resolver); | ||
assert.equal('Harry Potter', xpath.selectWithResolver('//testns:title/text()', doc, resolver)[0].nodeValue); | ||
assert.equal('JKR', xpath.selectWithResolver('//testns:field[@testns:type="author"]/text()', doc, resolver)[0].nodeValue); | ||
test.done(); | ||
}, | ||
'select xpath with namespaces, using namespace mappings': function (test) { | ||
var xml = '<book xmlns:testns="http://example.com/test"><testns:title>Harry Potter</testns:title><testns:field testns:type="author">JKR</testns:field></book>'; | ||
var doc = new dom().parseFromString(xml); | ||
var select = xpath.useNamespaces({'testns': 'http://example.com/test'}); | ||
assert.equal('Harry Potter', select('//testns:title/text()', doc)[0].nodeValue); | ||
assert.equal('JKR', select('//testns:field[@testns:type="author"]/text()', doc)[0].nodeValue); | ||
test.done(); | ||
}, | ||
'select attribute': function (test) { | ||
@@ -74,0 +106,0 @@ var xml = '<author name="J. K. Rowling"></author>'; |
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
130298
4008
76
0
1