goto100 / xpath

DOM 3 Xpath implemention and helper for node.js
MIT License
225 stars 71 forks source link

Selecting multiple NamespaceNodes throws 'Unexpected: could not determine node order' #83

Closed marklagendijk closed 11 months ago

marklagendijk commented 6 years ago
const xpath = require('xpath');
const DOMParser = require('xmldom').DOMParser;

const xml = '<book xmlns:book1="http://example.com/book1" xmlns:book2="http://example.com/book2"><title>Harry Potter</title></book>';
const document = new DOMParser().parseFromString(xml);
const nodes = xpath.select('/*/namespace::*', document);

console.log(nodes[0].localName + ': ' + nodes[0].firstChild.data);
console.log('Node: ' + nodes[0].toString());

Throws

Error: Unexpected: could not determine node order
    at nodeOrder (/test/node_modules/xpath/xpath.js:3021:11)
    at AVLTree.add (/test/node_modules/xpath/xpath.js:3029:10)
    at XNodeSet.buildTree (/test/node_modules/xpath/xpath.js:3147:23)
    at XNodeSet.toArray (/test/node_modules/xpath/xpath.js:3188:23)
    at new XPathResult (/test/node_modules/xpath/xpath.js:4391:20)
    at XPathExpression.evaluate (/test/node_modules/xpath/xpath.js:4316:9)
    at Object.exports.selectWithResolver (/test/node_modules/xpath/xpath.js:4738:26)
    at Object.exports.select (/test/node_modules/xpath/xpath.js:4718:17)
    at Object.<anonymous> (/test/test.js:6:21)
    at Module._compile (module.js:652:30)

The issue is that the function nodeOrder is not able to determine in which order it should return multiple NamespaceNodes, and therefore throws an Error.

JLRishe commented 11 months ago

I'm sorry that it has taken so long to fix this and I imagine the fix is probably no longer relevant to you, but it is fixed now in version 0.0.34. Thank you for reporting it.