XPath question: selecting content between two nodes

Discussion in 'XML' started by Yoav Shapira, Mar 7, 2006.

  1. Yoav Shapira

    Yoav Shapira Guest

    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
     
    Yoav Shapira, Mar 7, 2006
    #1
    1. Advertising

  2. In article <>,
    Yoav Shapira <> 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
     
    Richard Tobin, Mar 7, 2006
    #2
    1. Advertising

  3. 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)
     
    Johannes Koch, Mar 7, 2006
    #3
  4. Yoav Shapira

    Yoav Shapira Guest

    Thank you very much: this makes sense and I'll try it out.
     
    Yoav Shapira, Mar 8, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olivier Wulveryck
    Replies:
    1
    Views:
    706
    Andrew Thompson
    Sep 14, 2004
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,527
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    436
    Patrick TJ McPhee
    Jun 17, 2004
  4. Tjerk Wolterink
    Replies:
    9
    Views:
    1,357
    Joris Gillis
    Feb 7, 2005
  5. Replies:
    1
    Views:
    615
    Soren Kuula
    Aug 24, 2006
Loading...

Share This Page