Reputation: 917
I'm trying to find the first preceding-sibling
of a node with OR condition.
For example , i have a node A
and i try to get the first preceding-sibling B
but if there is no B
then get first preceding-siblingSPAN
.
I don't know how to do the OR condition:
This is what i tried :
A//preceding-sibling::B|SPAN[1]
A//preceding-sibling::B[1]|SPAN[1]
A//preceding-sibling::B[1]|preceding-sibling::SPAN[1]
A//(preceding-sibling::B[1]|preceding-sibling::SPAN[1]) // Xpath invalid
A//preceding-sibling::(B|SPAN)[1] // Xpath invalid
.... and many more
In short I'm looking for : Node -> preceding-sibling::NodeType1[1]
OR preceding-sibling::NodeType2[1]
Is there a way to achieve this with Xpath or i must try one and if there is no result try the other ?
EDIT:
The good answer to this question is I think StuartLC answer (I didn't try it).
If you are looking for the first node between B
and SPAN
then used har07 answer.
Upvotes: 2
Views: 425
Reputation: 107237
As per Michael's comment, note that *[name()='B' or name()='SPAN']
will return the first preceding-sibling
which is either B
or SPAN
.
If I understand your question correctly, you want to 'fallback' to SPAN
only IFF there are no B
preceding-sibling
s of A
at all. AFAIK you will need to repeat the query with a union:
(A/preceding-sibling::B[1] | A/preceding-sibling::SPAN[1])[1]
Upvotes: 2
Reputation: 89285
You can try this way instead :
A//preceding-sibling::*[name()='B' or name()='SPAN'][1]
Upvotes: 1