0
votes

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?

2
A lot of stuff missing here! - Stewie Griffin
It is not clear what you are asking. What do you simplify your HTML page to? And what result do you want to get? Please improve your question. - dirkk
sorry, I didn't put the 4 whitespaces before the lines of code so Stackoverflow hided tags and line returns. Corrected now. - Imbuter

2 Answers

0
votes

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()

0
votes
//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;')