A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

[Draft Idea/Spec/Proposal] API Web Javascript Xpath

raphaellouis
2022-05-22

Hi all!

Feature name

[Idea] API Web Javascript Xpath

Feature description

  1. I wish there was an api to parse html in a standardized way. Currently, I have to use third-party libraries for this.
  2. If one existed a standardized xpath api, I could create an extension on google, to analyze and send information from the site that I need easily and accurately
  3. There are several documentations made about this, but there is no minimalistic and easy api for this. Reference here: interface-xpathexpression or here DOM_XPath or here xpath spec or here DOM-Level-3-XPath or here 0310
before.js
var xpath = "//div";
var evaluator = new XPathEvaluator();
var expression = evaluator.createExpression(xpath);
var result = expression.evaluate(document, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
document.querySelector("output").textContent = result.snapshotLength;
after.js
var select, 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())
References