Skip to content

Functions queryPath and queryPathAll #161

@sjehuda

Description

@sjehuda

Greetings.

I am considering to use ltx dir a DOAP project.

I figured that it might be worth to share these couple of functions, and add them to this project.

These couple of functions enable the utilization of XPath with ECMAScript in a fashion which is similar to functions querySelector and querySelectorAll.

Node.prototype.queryPathAll = function (namespace, expression) {
  let data = this.ownerDocument || this;
  let nodes = data.evaluate(
    expression,
    this,
    () => namespace,
    XPathResult.ORDERED_NODE_ITERATOR_TYPE,
    null);
  let results = [];
  let node = nodes.iterateNext();
  while (node) {
    // Add the link to the array
    results.push(node);
    // Get the next node
    node = nodes.iterateNext();
  }
  return results;
};

Node.prototype.queryPath = function (namespace, expression) {
  let data = this.ownerDocument || this;
  return data.evaluate(
    expression,
    this,
    () => namespace,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null)
    .singleNodeValue;
};

Usage

A singular selection

Selecting element atom:feed/atom:link from an Atom Syndication Format document.

nodeFeed = xmlFile.queryPath(xmlns.atom, "atom:feed")
nodeNext = nodeFeed.queryPath(xmlns.atom, "atom:link[@rel='next']");
nodePrevious = nodeFeed.queryPath(xmlns.atom, "atom:link[@rel='previous']");

Multiple selection

Selecting element atom:feed/atom:entry from an Atom Syndication Format document.

let nodesEntry = nodeFeed.queryPathAll(xmlns.atom, "atom:entry");
for (const nodeEntry of nodesEntry) {
  // Additional code.
}

Namespace

Defining XML namespaces.

xmlns = {
  "atom"      : "http://www.w3.org/2005/Atom",
  "content"   : "http://purl.org/rss/1.0/modules/content/",
  "dc"        : "http://purl.org/dc/elements/1.1/",
  "doap"      : "http://usefulinc.com/ns/doap#",
  "foaf"      : "http://xmlns.com/foaf/0.1/",
  "geo"       : "http://www.w3.org/2003/01/geo/wgs84_pos#",
  "metalink"  : "http://www.metalinker.org/",
  "metalink4" : "urn:ietf:params:xml:ns:metalink",
  "osd"       : "http://a9.com/-/spec/opensearch/1.1/",
  "owl"       : "http://www.w3.org/2002/07/owl#",
  "podcast"   : "https://podcastindex.org/namespace/1.0",
  "rdf"       : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  "rdfs"      : "http://www.w3.org/2000/01/rdf-schema#",
  "rss"       : "http://purl.org/rss/1.0/",
  "schema"    : "https://schema.org/",
  "sitemap"   : "http://www.sitemaps.org/schemas/sitemap/0.9",
  "smf"       : "http://www.simplemachines.org/xml/recent",
  "sy"        : "http://purl.org/rss/1.0/modules/syndication/",
  "tf"        : "urn:schapps:params:xml:ns:thefocus",
  "xhtml"     : "http://www.w3.org/1999/xhtml",
  "xlink"     : "http://www.w3.org/1999/xlink",
  "xmpp"      : "https://linkmauve.fr/ns/xmpp-doap#",
  "xsl"       : "http://www.w3.org/1999/XSL/Transform",
  "xul"       : "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  "xup"       : "http://www.w3.org/2002/03/xup"
}

gemini://woodpeckersnest.space/~schapps/tutorial/2025-10-26-selecting-xml-html-or-json-nodes-with-xpath-utilizing-ecmascript.gmi

https://greasyfork.org/en/discussions/development/313224-this-code-would-allow-you-to-query-for-elements-with-xpath-query

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions