XPathResult: singleNodeValue プロパティ

singleNodeValueXPathResult インターフェイスの読み取り専用プロパティで、 Node 値を返すか、XPathResult.resultTypeANY_UNORDERED_NODE_TYPE または FIRST_ORDERED_NODE_TYPE である結果のノードが一致しなかった場合は null を返します。

返値は Document.evaluate() が返す XPathResultNode 値です。

例外

TYPE_ERR

XPathResult.resultTypeANY_UNORDERED_NODE_TYPE または FIRST_ORDERED_NODE_TYPE でない場合、 TYPE_ERR 型の XPathException が発生します。

次の例では singleNodeValue プロパティを使用しています。

HTML

html
<div>XPath example</div>
<div>
  Tag name of the element having the text content 'XPath example':
  <output></output>
</div>

JavaScript

js
const xpath = "//*[text()='XPath example']";
const result = document.evaluate(
  xpath,
  document,
  null,
  XPathResult.FIRST_ORDERED_NODE_TYPE,
  null,
);
document.querySelector("output").textContent = result.singleNodeValue.localName;

結果

仕様書

Specification
DOM Standard
# dom-xpathresult-singlenodevalue

ブラウザーの互換性

BCD tables only load in the browser