REXML XPath bug?

Discussion in 'Ruby' started by Andy Watts, May 14, 2008.

  1. Andy Watts

    Andy Watts Guest

    I'm having trouble with a particular XPATH in REXML and would greatly
    appreciate any help.

    Given this simple document..
    <body>
    <div class="summary">xxx</div>
    <div>
    <span>yyy</span>
    </div>
    </body>

    The xpath "//div[@class='summary']/following-sibling::div/span" is
    failing to return '<span>yyy</span>'.


    Here's a console dump that also shows a simplier xpath working.

    >> REXML::XPath.match(doc, "//div[@class='summary']/following-sibling::div/span").to_s

    => ""
    >> REXML::XPath.match(doc, "//div[@class='summary']/following-sibling::div").to_s

    => "<div>\n<span>yyy</span>\n</div>"
    >> puts doc.to_s

    <body>
    <div class='summary'>xxx</div>
    <div>
    <span>yyy</span>
    </div>
    </body>

    The xpath works fine in firefox's xpath checker, so it seems like an
    issue with rexml.
    Any help greatly appreciated.

    - Andy
    --
    Posted via http://www.ruby-forum.com/.
    Andy Watts, May 14, 2008
    #1
    1. Advertising

  2. Andy Watts

    Casimir Guest

    On Wed, 14 May 2008 15:00:47 -0500, Andy Watts wrote:

    > I'm having trouble with a particular XPATH in REXML and would greatly
    > appreciate any help.

    (rest of it quoted below)

    Ok, I tested this with JEdits XPATH tool and indeed, the xpath "//div
    [@class='summary']/following-sibling::div" returns the span-element with
    string-value yyy, xml fragment <span>yyy</span>.

    Is it a bug? I must admit I would need to read more references etc before
    being able to say definately - or is it just different interpretation of
    the following-sibling -axis. Dont have the time to see the rfc atm.

    But I played around a bit:
    irb(main):046:0> puts root.elements["//div[@class='summary']/following-
    sibling::div/span"]
    nil
    * So, it doesnt work like that.

    irb(main):047:0> puts root.elements["//div[@class='summary']/following-
    sibling::div"]
    <div>
    <span>yyy</span>
    </div>
    * hmm....

    irb(main):049:0> puts root.elements["//div[@class='summary']/
    following::div/span"]
    <span>yyy</span>
    ** Would this work in your case?

    ALSO - you could include the rexml namespace by adding the line
    include REXML
    after the rexml requires. This way you wont have to use the long ::-
    syntax, but can refer to document or elements directly, and use xpaths
    simply as above inside [""].

    I also played around with this http://www.zvon.org:9001/saxon/cgi-bin/
    XLab/XML/xpatut_15.html and following seems to accomplish what you need.

    Finally, just plain-jane /div/span would select the span but maybe there
    can be many and you only want the one after the summary div.

    Out of time, gtg

    Casimir Pohjanraito

    > Given this simple document..
    > <body>
    > <div class="summary">xxx</div>
    > <div>
    > <span>yyy</span>
    > </div>
    > </body>
    >
    > The xpath "//div[@class='summary']/following-sibling::div/span" is
    > failing to return '<span>yyy</span>'.
    >
    >
    > Here's a console dump that also shows a simplier xpath working.
    >
    >>> REXML::XPath.match(doc,
    >>> "//div[@class='summary']/following-sibling::div/span").to_s

    > => ""
    >>> REXML::XPath.match(doc,
    >>> "//div[@class='summary']/following-sibling::div").to_s

    > => "<div>\n<span>yyy</span>\n</div>"
    >>> puts doc.to_s

    > <body>
    > <div class='summary'>xxx</div>
    > <div>
    > <span>yyy</span>
    > </div>
    > </body>
    >
    > The xpath works fine in firefox's xpath checker, so it seems like an
    > issue with rexml.
    > Any help greatly appreciated.
    >
    > - Andy
    Casimir, May 17, 2008
    #2
    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. Damphyr
    Replies:
    2
    Views:
    137
    Damphyr
    Jul 16, 2003
  2. Alexey Verkhovsky
    Replies:
    0
    Views:
    176
    Alexey Verkhovsky
    Aug 3, 2004
  3. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    150
    Henrik Horneber
    Oct 12, 2004
  4. Eric Armstrong

    REXML XPath: bug or misunderstanding?

    Eric Armstrong, Aug 2, 2006, in forum: Ruby
    Replies:
    14
    Views:
    196
    Eric Armstrong
    Aug 13, 2006
  5. altinsel

    ruby / rexml / xpath bug?

    altinsel, Sep 15, 2008, in forum: Ruby
    Replies:
    7
    Views:
    189
    Mark Thomas
    Sep 16, 2008
Loading...

Share This Page