XPath question: selecting content between two nodes

Y

Yoav Shapira

Hi,
I have a well-formed XHTML document that, among other content, has the
following segment:

<img src="uniqueUrl1" />
<p>some text</p>
<p>some other text</p>
<p>some more text</p>
<img src="uniqueUrl2" />

Now, my question: given unique URLs 1 and 2, how do I select all the
content between the two img nodes?

I thought //img[@src="uniqueUrl1"] would work for the first image and
//img[@src="uniqueUrl2"] for the second image node, but they don't seen
to, and anyways they don't give me the content between. I know of the
following axis, but not sure how to use it if it's applicable here at
all.

Any help would be appreciated -- thank you!

Yoav Shapira
 
R

Richard Tobin

Yoav Shapira said:
<img src="uniqueUrl1" />
<p>some text</p>
<p>some other text</p>
<p>some more text</p>
<img src="uniqueUrl2" />
Now, my question: given unique URLs 1 and 2, how do I select all the
content between the two img nodes?
I thought //img[@src="uniqueUrl1"] would work for the first image and
//img[@src="uniqueUrl2"] for the second image node, but they don't seen
to

Well, I can't solve that problem, because those XPaths *do* select
the nodes in question! If it's not working for you, you are using them
wrongly.
and anyways they don't give me the content between. I know of the
following axis, but not sure how to use it if it's applicable here at
all.

Something like this will select the nodes between the two <img>
elements. It uses the following-sibling axis to get the sibling nodes
after the first <img>, and restricts them to the ones that have the
second <img> as a following-sibling.

//img[@src="uniqueUrl1"]/following-sibling::node()[following-sibling::img[@src="uniqueUrl2"]]

-- Richard
 
J

Johannes Koch

Yoav said:
Hi,
I have a well-formed XHTML document that, among other content, has the
following segment:

<img src="uniqueUrl1" />
<p>some text</p>
<p>some other text</p>
<p>some more text</p>
<img src="uniqueUrl2" /> [...]
I thought //img[@src="uniqueUrl1"] would work for the first image and
//img[@src="uniqueUrl2"] for the second image node, but they don't seen
to

Maybe, as so often, it's a namespace issue. XHTML uses a namespace. So
the XPath expressions must use the same namespace, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top