Ruby libxml annoyances

>> doc.find('/*').first.name
doc.find('/*').first.name
=> "Customer"
>> doc.find('/Customer').first.name
doc.find('/Customer').first.name
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name
	from (irb):52

Something is wrong with Ruby’s libxml xpath evaluator. I’m only able to use the wildcard * to select elements and not the element’s name. After doing some research on the project’s page, there are 2 comments about the problem, complaining that libxml’s support of XPath is just too limited and it has lots of problems when dealing with namespaces. Default namespaces are not parsed, namespace wildcards are not supported and namespace prefixes won’t work all the time. What’s the advantage of announcing a 300x faster xpath speed than rexml when it doesn’t work and valid xpath expressions return syntax error?

>> doc.find('/*:Customer').first.name
doc.find('/*:Customer').first.name
XML::XPath::InvalidPath: Invalid XPath expression
	from (irb):73:in `find'
	from (irb):73

It’s time to go back to the slower but working rexml.

VN:F [1.8.1_1037]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.1_1037]
Rating: 0 (from 0 votes)

Popularity: 9% [?]

Tags: , . 

Related Entries:
  • Ruby and libxml in Windows
  • DB2 on Rails update
  • XML Mapping in Ruby
  • Recent readings
  • Ruby declared TIOBE’s Programming Language of 2006


  • 2 Responses to “Ruby libxml annoyances”

    1. Vitor Rodrigues » Blog Archive » DB2 on Rails update Says:

      [...] back to fiddling around with my Ruby on Rails experiments(1)(2). I was able to create a very useful 2-way mapping between Ruby objects and xml data stored in [...]

    2. Jim Harpy Says:

      You can use the following syntax to resolve this issue:

      parser.find(“//*[local-name()='NODENAME']“)

      UN:F [1.8.1_1037]
      Rating: 0.0/5 (0 votes cast)
      UN:F [1.8.1_1037]
      Rating: 0 (from 0 votes)

    Leave a Reply