Skip to content

Commit

Permalink
run xpath on non document element nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
yaronn committed Aug 25, 2012
1 parent bb0cbfd commit 05b6d71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "xpath.js",
"version": "0.0.1",
"version": "0.0.3",
"description": "Xpath pure javascript implementation for node.js",
"engines": { "node": ">=0.4.0" },
"author": "Cameron McCormack (http://mcc.id.au/xpathjs), Yaron Naveh ([email protected], http://webservices20.blogspot.com/)",
"dependencies": {

},
"devDependencies": {
"nodeunit": ">=0.6.4",
"nodeunit": ">=0.6.4"
},
"repository" : {
"type":"git",
Expand Down
8 changes: 8 additions & 0 deletions sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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())
19 changes: 12 additions & 7 deletions xpath.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* xpath.js
*
* An XPath 1.0 library for JavaScript.
Expand Down Expand Up @@ -4278,12 +4278,17 @@ try {

function SelectNodes(doc, xpath)
{
var parser = new XPathParser();
var xpath = parser.parse(xpath);
var context = new XPathContext();
context.expressionContextNode = doc.documentElement;
var res = xpath.evaluate(context)
return res.toArray();
var parser = new XPathParser();
var xpath = parser.parse(xpath);
var context = new XPathContext();
if(doc.documentElement){
context.expressionContextNode = doc.documentElement;
} else {
context.expressionContextNode = doc;
}
var res = xpath.evaluate(context)

return res.toArray();
}

module.exports = SelectNodes;

0 comments on commit 05b6d71

Please sign in to comment.