On the Firefox web console
<h1><span class='A'>text1</span>text2</span>text2>/h1>
For the h1
element,
document.value("span[@class='A']|.", <nodes representing h1 elements >, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerText
called
"Text 1 Text 2"
I got the text.Can I assume that this is because .
matches the first node in Document order instead of span[@class='A']
?
Also, can a single XPath be used to prioritize nodes that match xpath1
regardless of the document order and return nodes that match xpath2
if no such node exists?
(As mentioned above, xpath1|xpath2
was not enough.I will return the set, so it is only natural.)
I wonder if it's like this.
'use strict';
/**
* Get the first matching h1 element node (context node)
*/
var h1 = document.value("span[@class='A']|.", document.querySelector('h1'), null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
console.log(h1);//<h1><span class='A'>text1</span>text2</span>/h1>
/**
* Obtain element nodes except root nodes (context nodes)
*/
varspan=document.value("span[@class='A']|descendant::*", document.querySelector('h1'), null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
console.log(span);//<span class='A'>text1</span>
/**
* If it does not match span [@class='A'],
*/
var node=document.value("span[@class='A']", document.querySelector('h1'), null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if(!node){
node = document.evaluate("."", document.querySelector('h1'), null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
console.log(node);//<span class='A'>text1</span>
<h1><span class='A'>text1</span>text2</span>text2>/h1>
What about xpath1|xpath2[not(xpath1)]
?
(In XPath 2.0 and later, xpath1
, xpath2
are both single nodes, although (xpath1,xpath2)[1]
is standard.)
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
581 PHP ssh2_scp_send fails to send files as intended
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.