Use an svg
as a temp element if you need to run XPath against XML, since security restrictions prevent evaluating XPath expressions on an element not attached to the DOM:
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>XPath Context</title>
<meta charset="utf-8">
</head>
<body>
<svg id="model" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"">
<?foo bar?>
</svg>
<script type="text/javascript;version=1.7">
function $xpath(aXPath, aContext)
{
let nodes = [];
aContext = document.getElementById("model") || doc;
let results = document.evaluate(aXPath, aContext, null, XPathResult.ANY_TYPE, null);
let node;
while ((node = results.iterateNext())) {
nodes.push(node);
}
return nodes;
}
</script>
</body>
</html>
Or use a JavaScript implementation.
document.createDocumentFragment
with DOM nodes, or is this XML, there's not really much context here ? – Yak