Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select nth child in xQuery / select next element

Tags:

select

xquery

I am having trouble finding good xQuery tutorials, basically what I am trying to do is retreive the text etc... from this html node

<div class="venue">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">ADDRESS:</p>
        <p class="item-big">blabla</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">PHONE:</p>
        <p class="item-big">123</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">WEB:</p>
        <p class="item-big">etc...</p>
    </div><br class="clear">
</div>

I'd like to know how can I get the data out of the 2nd p in the third div[@class="vitem"]
Or the p directly following the p[@class="label"] that contains the text WEB:

Edit: Answeres already helped a great bit, however my second question is if the layout changes to something like this

<div class="venue">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">ADDRESS:</p>
        <p class="item-big">blabla</p>
    </div><br class="clear">
    <div class="vitem">
        <p style="padding: 6px 0pt 0pt;" class="label">WEB:</p>
        <p class="item-big">etc...</p>
    </div><br class="clear">
</div>

How do i get the etc..., knowing only that it follows a p with the class label containing the text WEB:? it's no longer in div[3]/p[2]

Thanks!

like image 377
Moak Avatar asked Dec 01 '25 05:12

Moak


1 Answers

I'd like to know how can I get the data out of the 2nd p in the third div[@class="vitem"]

Use:

/*/div[@class='vitem'][3]/p[2]/text()

This means: get all the text node children of the second p child of the third of all div elements that have an attribute class with value "vitem" and that are children of the top element.

Or the p directly following the p[@class="label"] with the data WEB:

Use:

/*/div[@class='vitem'][3]/p[@class='label']
                             /following-sibling::p[1]/text()

This means: get all the text node children of the first following-sibling p of any p element with class attribute with value "label", that is a child of the third of all div elements that have an attribute class with value "vitem" and that are children of the top element.

UPDATE: The OP has added a second question: he wants just to select the p that has as a string value the text "etc..."

Use:

/*/div/p[.='etc...']
like image 109
Dimitre Novatchev Avatar answered Dec 04 '25 04:12

Dimitre Novatchev