XPathResult: singleNodeValue プロパティ
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。
singleNodeValue は XPathResult インターフェイスの読み取り専用プロパティで、 Node 値を返すか、XPathResult.resultType が ANY_UNORDERED_NODE_TYPE または FIRST_ORDERED_NODE_TYPE である結果のノードが一致しなかった場合は null を返します。
値
返値は Document.evaluate() が返す XPathResult の Node 値です。
例外
TYPE_ERR
XPathResult.resultType が ANY_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;
結果
仕様書
| 仕様書 |
|---|
| DOM> # dom-xpathresult-singlenodevalue> |