How to make one RxPath from two
I have those two RxPaths which I need to be written in one expresion:
/td[2]/a[1]/tag[1]
and
/td[2]/a[1]
So basically I need to select path with 'tag' element if exists, if not than to select 'a' element. something like:
if exist /td[2]/a[1]/tag[1] select /td[2]/a[1]/tag[1]
else select /td[2开发者_StackOverflow中文版]/a[1]
Those elements need to have innertext attribute with some value in them, so I tried:
/td[2]/descendant::node()[@innertext!='']
but it won't work...
Also those elements are at the bottom of hierarchy so if is there any way to just select first element at lowest level.
I managed to solve this with an regex at the end of my Xpath expression.
/dom/body/div[@id='isc_0']/div/div[@id='isc_B']/div[@id='isc_C']/div[@id='isc_10']/div/div/iframe/body/table/tbody/tr/td[1]/a[@innertext='any uri item']/../../td[2]/*[@innertext~'[^ ]+']
Sorry for misunderstanding with problem...
Regards,
Vajda Vladimir
So basically I need to select path with 'tag' element if exists, if not than to select 'a' element. something like:
if exist
/td[2]/a[1]/tag[1]
select
/td[2]/a[1]/tag[1]
else select
/td[2]/a[1]
I highly doubt that the top element of the document is a td
. Don't use /td
-- it means you want to select the top element of the document and this top element must be a td
.
Also, /td[2]
never selects anything, because a (wellformed) XML document has exactly one top element.
Use:
someParentElement/td[2]/a[1]/tag[1]
|
someParentElement/td[2]/a[1][not(someParentElement/td[2]/a[1]/tag[1])]
Those elements need to have innertext attribute with some value in them
Use:
someParentElement/td[2][.//@innertext[normalize-space()]]/a[1]/tag[1]
|
someParentElement/td[2]
[.//@innertext[normalize-space()]]/a[1]
[not(someParentElement/td[2]
[.//@innertext[normalize-space()]]/a[1]/tag[1])]
Also those elements are at the bottom of hierarchy so if is there any way to just select first element at lowest level.
This is not clear. Please, clarify.
All "leaf" elements can be selected using the following XPath expression:
//*[not(*)]
The elements selected don't have any children-elements, but may have other children (such as text-nodes, PIs, comments) and attributes.
Besides all those good advices from @Dimitre, I want to add that a parent will always come before (in document order) than a child, so you could use this XPath expression:
(/real-path-from-root/td[2]/a[1]
|
/real-path-from-root/td[2]/a[1]/tag[1])[last()]
You could do this without |
union set operator in XPath 1.0, but it will end up very unreadable... Of course, in XPath 2.0 you could just do:
(/real-path-from-root/td[2]/a[1]/(.|tag[1]))[last()]
精彩评论