Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath question: selecting content between two nodes

Reply
Thread Tools

XPath question: selecting content between two nodes

 
 
Yoav Shapira
Guest
Posts: n/a
 
      03-07-2006
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

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      03-07-2006
In article <(E-Mail Removed). com>,
Yoav Shapira <(E-Mail Removed)> wrote:

><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
 
Reply With Quote
 
 
 
 
Johannes Koch
Guest
Posts: n/a
 
      03-07-2006
Yoav Shapira wrote:
> 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.
--
Johannes Koch
Spem in alium nunquam habui praeter in te, Deus Israel.
(Thomas Tallis, 40-part motet)
 
Reply With Quote
 
Yoav Shapira
Guest
Posts: n/a
 
      03-08-2006
Thank you very much: this makes sense and I'll try it out.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Nodes Using Subtotal of Child Nodes renfrochris@hotmail.com XML 1 08-24-2006 12:30 AM
XPATH: Selecting child nodes wich are NOT equal to Tjerk Wolterink XML 9 02-07-2005 08:47 PM
Problem with XPath JDOM: Always the same value returned when selecting different nodes. Olivier Wulveryck Java 1 09-14-2004 01:34 PM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments