yaronn / xpath.js

An xpath module for node, written in pure javascript
103 stars 100 forks source link

Can't make xpath on a previous xpath result #2

Closed cvillemure closed 12 years ago

cvillemure commented 12 years ago

Assuming I have a XML with a list of collection. I would first make a XPath to extract all collections from my XML and then loop trough the resulting list of collections to parse those collections. Unfortunately with your original code, it is impossible to call XPath again on the resulting nodes.

However after checking the differences between a "doc" element and a result node, i realized they were almost the same...

A simple modification to your "SelectNodes" function does the trick :

function SelectNodes(doc, xpath) { 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();
}

yaronn commented 12 years ago

@cvillemure - thanks, I've added this code