REXML 3.1.6 has XPath problems

Discussion in 'Ruby' started by Kouhei Sutou, Dec 30, 2006.

  1. Kouhei Sutou

    Kouhei Sutou Guest

    ----Next_Part(Sat_Dec_30_13_00_32_2006_860)--
    Content-Type: Multipart/Mixed;
    boundary="--Next_Part(Sat_Dec_30_13_00_32_2006_860)--"

    ----Next_Part(Sat_Dec_30_13_00_32_2006_860)--
    Content-Type: Text/Plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit

    Hi,

    I found some bugs of REXML 3.1.6 related XPath.

    The following code raises an exception:

    require 'rexml/document'

    source = <<-XML
    <a>
    <b number='1' str='abc'>TEXT1</b>
    <c number='1'/>
    <c number='2' str='def'>
    <b number='3'/>
    <d number='1' str='abc'>TEXT2</d>
    <b number='2'><!--COMMENT--></b>
    </c>
    </a>
    XML
    doc = REXML::Document.new(source)

    predicate = "count(child::node()|following-sibling::node()|preceding-sibling::node())=0"
    p REXML::XPath.match(doc, "/descendant-or-self::node()[#{predicate}]")

    The exception is the following:
    /usr/lib/ruby/1.8/rexml/xpath_parser.rb:335:in `expr': undefined method `children' for nil:NilClass (NoMethodError)
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:334:in `each'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:334:in `expr'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:422:in `expr'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:421:in `expr'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:447:in `expr'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:445:in `collect'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:445:in `expr'
    from /usr/lib/ruby/1.8/rexml/encoding.rb:47:in `each_with_index'
    ... 14 levels...
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:125:in `match'
    from /usr/lib/ruby/1.8/rexml/xpath_parser.rb:56:in `parse'
    from /usr/lib/ruby/1.8/rexml/xpath.rb:63:in `match'
    from rexml-3.1.6-xpath-bug.rb:21


    I'll attach a patch of test case and fixing.

    Thanks,
    --
    kou

    ----Next_Part(Sat_Dec_30_13_00_32_2006_860)--
    Content-Type: Text/Plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline; filename="rexml-3.1.6-siblings-bug.diff"

    diff -ru rexml_3.1.6.orig/src/rexml/xpath_parser.rb rexml_3.1.6/src/rexml/xpath_parser.rb
    --- rexml_3.1.6.orig/src/rexml/xpath_parser.rb 2006-11-28 22:54:57.000000000 +0900
    +++ rexml_3.1.6/src/rexml/xpath_parser.rb 2006-12-30 12:55:45.000000000 +0900
    @@ -351,7 +351,8 @@
    when :following_sibling
    #puts "FOLLOWING_SIBLING 1: nodeset = #{nodeset}"
    results = []
    - for node in nodeset
    + nodeset.each do |node|
    + next if node.parent.nil?
    all_siblings = node.parent.children
    current_index = all_siblings.index( node )
    following_siblings = all_siblings[ current_index+1 .. -1 ]
    @@ -362,13 +363,14 @@

    when :preceding_sibling
    results = []
    - for node in nodeset
    + nodeset.each do |node|
    + next if node.parent.nil?
    all_siblings = node.parent.children
    current_index = all_siblings.index( node )
    - preceding_siblings = all_siblings[ 0 .. current_index-1 ].reverse
    - #results += expr( path_stack.dclone, preceding_siblings )
    + preceding_siblings = all_siblings[ 0, current_index ].reverse
    + results += preceding_siblings
    end
    - nodeset = preceding_siblings || []
    + nodeset = results
    node_types = ELEMENTS

    when :preceding
    diff -ru rexml_3.1.6.orig/test/xpath_test_pred.rb rexml_3.1.6/test/xpath_test_pred.rb
    --- rexml_3.1.6.orig/test/xpath_test_pred.rb 2006-11-28 22:54:56.000000000 +0900
    +++ rexml_3.1.6/test/xpath_test_pred.rb 2006-12-30 12:53:44.000000000 +0900
    @@ -54,4 +54,27 @@
    #puts path, @parser.parse( path ).inspect
    return m
    end
    +
    + def test_get_no_siblings_terminal_nodes
    + source = <<-XML
    +<a>
    + <b number='1' str='abc'>TEXT1</b>
    + <c number='1'/>
    + <c number='2' str='def'>
    + <b number='3'/>
    + <d number='1' str='abc'>TEXT2</d>
    + <b number='2'><!--COMMENT--></b>
    + </c>
    +</a>
    +XML
    + doc = REXML::Document.new(source)
    + predicate = "count(child::node()|" +
    + "following-sibling::node()|" +
    + "preceding-sibling::node())=0"
    + m = REXML::XPath.match(doc, "/descendant-or-self::node()[#{predicate}]")
    + assert_equal( [REXML::Text.new("TEXT1"),
    + REXML::Text.new("TEXT2"),
    + REXML::Comment.new("COMMENT")],
    + m )
    + end
    end

    ----Next_Part(Sat_Dec_30_13_00_32_2006_860)----
    ----Next_Part(Sat_Dec_30_13_00_32_2006_860)----
     
    Kouhei Sutou, Dec 30, 2006
    #1
    1. Advertising

  2. Kouhei Sutou

    vasudevram Guest

    vasudevram, Dec 30, 2006
    #2
    1. Advertising

  3. Kouhei Sutou

    Kouhei Sutou Guest

    Hi,

    In <>
    "Re: REXML 3.1.6 has XPath problems" on Sun, 31 Dec 2006 02:55:09 +0900,
    "vasudevram" <> wrote:

    > Kouhei Sutou wrote:
    > >

    >
    > What are the problems that you've seen?
    > Like to know as I use REXML. Don't remember the version I use right
    > now, will check.


    My last mail was broken. You can see the (broken) mail in
    blade:
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/231756

    Thanks,
    --
    kou
     
    Kouhei Sutou, Dec 31, 2006
    #3
  4. Kouhei Sutou

    vasudevram Guest

    Kouhei Sutou wrote:
    > >
    > > What are the problems that you've seen?
    > > Like to know as I use REXML. Don't remember the version I use right
    > > now, will check.

    >
    > My last mail was broken. You can see the (broken) mail in
    > blade:
    > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/231756
    >
    > Thanks,
    > --
    > kou


    Thanks for the reply.
    Vasudev
     
    vasudevram, Jan 2, 2007
    #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. Damphyr
    Replies:
    2
    Views:
    141
    Damphyr
    Jul 16, 2003
  2. Han Holl

    Rexml xpath question

    Han Holl, Feb 22, 2004, in forum: Ruby
    Replies:
    7
    Views:
    143
    Han Holl
    Mar 6, 2004
  3. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    153
    Henrik Horneber
    Oct 12, 2004
  4. Kouhei Sutou

    REXML 3.1.6 has a XPath parser problem

    Kouhei Sutou, Jan 6, 2007, in forum: Ruby
    Replies:
    0
    Views:
    107
    Kouhei Sutou
    Jan 6, 2007
  5. Phlip
    Replies:
    0
    Views:
    143
    Phlip
    Jan 15, 2008
Loading...

Share This Page