{"id":1332,"date":"2021-07-19T04:17:59","date_gmt":"2021-07-19T04:17:59","guid":{"rendered":"https:\/\/www.ishygddt.xyz\/~blog\/?p=1332"},"modified":"2023-02-15T09:47:40","modified_gmt":"2023-02-15T15:47:40","slug":"javascript-getelementbyxpath","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/07\/javascript-getelementbyxpath","title":{"rendered":"Javascript: getElementByXPath"},"content":{"rendered":"<pre><code class=\"language-javascript\" data-line=\"\">function getElementByXPath(path, context=null, document=null, namespaceResolver=null, first_ordered=true) {\n  if (context === null) context = window.document.documentElement;\n  if (document === null) document = context.ownerDocument;\n  if (namespaceResolver === null) namespaceResolver = document.createNSResolver(context);\n  const result = document.evaluate(\n    path,\n    context,\n    namespaceResolver,\n    first_ordered ? XPathResult.FIRST_ORDERED_NODE_TYPE : XPathResult.ANY_UNORDERED_NODE_TYPE,\n    null);\n  return result.singleNodeValue || null;\n}\n\nfunction getElementsByXPath(path, context=null, document=null, namespaceResolver=null, ordered=true) {\n  if (context === null) context = window.document.documentElement;\n  if (document === null) document = context.ownerDocument;\n  if (namespaceResolver === null) namespaceResolver = document.createNSResolver(context);\n  const result = document.evaluate(\n    path,\n    context,\n    namespaceResolver,\n    ordered ? XPathResult.ORDERED_NODE_SNAPSHOT_TYPE : XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,\n    null);\n  return Array.from(new Proxy(result, {\n    get(target, property, receiver) {\n      if (typeof property === &quot;string&quot;) {\n        if (property === &quot;length&quot;) return target.snapshotLength;\n        if (!isNaN(property)) return target.snapshotItem(property);\n      }\n      return Reflect.get(...arguments);\n    }\n  }));\n}\n\n\/\/ Alternate possibilities:\n\nfunction getElementsByXPath(path, context=null, document=null, namespaceResolver=null, ordered=true, snapshot=true) {\n  if (context === null) context = window.document.documentElement;\n  if (document === null) document = context.ownerDocument;\n  if (namespaceResolver === null) namespaceResolver = document.createNSResolver(context);\n  if (!snapshot) {\n    \/\/ Optional codepath to allow streaming iteration\n    const result = document.evaluate(\n      path,\n      context,\n      namespaceResolver,\n      ordered ? XPathResult.ORDERED_NODE_ITERATOR_TYPE : XPathResult.UNORDERED_NODE_ITERATOR_TYPE,\n      null);\n    return Object.assign(result, ({\n      next() {\n        const value = this.iterateNext();\n        return { value, done: !value };\n      },\n      [Symbol.iterator]() {\n        return this;\n      }\n    }));\n  }\n  const result = document.evaluate(path,\n    context,\n    namespaceResolver,\n    ordered ? XPathResult.ORDERED_NODE_SNAPSHOT_TYPE : XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,\n    null);\n  return Array.from(new Proxy(result, {\n    get(target, property, receiver) {\n      if (typeof property === &quot;string&quot;) {\n        if (property === &quot;length&quot;) return target.snapshotLength;\n        if (!isNaN(property)) return target.snapshotItem(property);\n      }\n      return Reflect.get(...arguments);\n  \/*\n  \/\/ TODO compare performance:\n  return Array.from(Object.assign(result, {\n    [Symbol.iterator]() {\n      var i = 0;\n      return {\n        next() {\n          const value = this.snapshotItem(i++);\n          return { value, done: !value };\n        }\n      };\n    }\n  }));\n  *\/\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<pre><code class=\"language-javascript\" data-line=\"\">function getElementByXPath(path, context=null, document=null, namespaceResolver=null, first_ordered=true) {\n  if (context === null) context = window.document.documentElement;\n  if (document === null) document = context.ownerDocument;\n  if (namespaceResolver === null) namespaceResolver = document.createNSResolver(context);\n  const result = document.evaluate(path, context, namespaceResolver, first_ordered ? XPathResult.FIRST_ORDERED_NODE_TYPE : XPathResult.ANY_UNORDERED_NODE_TYPE, null);\n  return result.singleNodeValue || null;\n}<\/code><\/pre>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[40],"class_list":["post-1332","post","type-post","status-publish","format-standard","hentry","category-drafts","tag-javascript"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=1332"}],"version-history":[{"count":31,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1332\/revisions"}],"predecessor-version":[{"id":2531,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1332\/revisions\/2531"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=1332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=1332"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=1332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}