Imbuter
Imbuter

Reputation: 17

XPATH/XQUERY for returning all the values one for line?

I have an html page that I can simplify like this:

<div id="foo" class="bar">one</div>
<div id="foo" class="bar">two</div>
<div id="foo" class="bar">three</div>

What's the XPATH/XQUERY that returns only those three values one for line?

one
two
three

update: so far the nearest solution that I see is this:

//div[@id='foo']/text()

how can I also add 'AND class="bar"' and a line return after each result?

Upvotes: 0

Views: 708

Answers (2)

dirkk
dirkk

Reputation: 6218

//div[@id='foo and class='bar']/text()

Please note that your input is not valid XML as XML has to have one root node.

To return your result on separate lines you should use the serialization options by your XPath/XQuery processor. This heavily depends on the processor you are using, which you did not specify. However, you can use a XQuery to concatenate your result with a line ending character, but this is rather ugly and bad practice.

for $x in //div[@id='foo and class='bar']/text()
return concat($x, '&#x000A;')

Upvotes: 0

hr_117
hr_117

Reputation: 9627

Your html is far to much simplified.
Question is what does unique identify your "three value".
If there are only three div's with class bar in your html this will do:

//div[@class='bar']

IF there are only three div's in the document also //div will do.

But the best way would be to have unique id's like:

<div id="foo" class="bar">one</div>
<div id="foo1" class="bar">two</div>
<div id="foo2" class="bar">three</div>

Than you can do:

//div[@id='foo' or @id='foo1' or @id='foo2']

Add text() if only the text content is wonted:

//div[@id='foo' or @id='foo1' or @id='foo2']/text()

Upvotes: 0

Related Questions