Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath how to select an element based on an order and dependent of its existence

Tags:

xml

xpath

how to select an element based on an order and dependent of its existence in XPath ? For example how to select the best quality video if it exist.

<VIDEOS>
    <LOW_RES>video_L.flv</LOW_RES>
    <HI_RES>video_H.flv</HI_RES>
    <HD/>
</VIDEOS>

this should return video_H.flv because the hd version doesn't exist

this case can exist (the videos names can be random):

<VIDEOS>
    <LOW_RES>video_L.flv</LOW_RES>
    <HI_RES>video_H.flv</HI_RES>
    <HD>video_hd.mp4</HD>
</VIDEOS>

this should return video_hd.mp4 because the hd version exist. Many thanks.

like image 494
hokkos Avatar asked Nov 05 '22 14:11

hokkos


1 Answers

Use:

/*/HD[text()]
|
 /*/HI_RES[text() and not(../HD/text())]
|
 /*/LOW_RES[text() and not(../HD/text()) and not(../HI_RES/text())]
like image 185
Dimitre Novatchev Avatar answered Nov 15 '22 11:11

Dimitre Novatchev