REXML Language Identification monkey patch


/ Published in: Ruby
Save to your folder(s)

REXML does not seem to have a 'lang' method, which is strange since lang is in the XML 1.0 Specification §2.12 'Language Identification', and in many other libraries. Because of Ruby's 'monkey patching', it's pretty easy to add - you just recursively browse the ancestors. Alas, because attributes are stored internally as strings and not an Attribute object, there does not seem to be any way of monkey patching §2.12 support on to REXML's attribute handling.

One day, Ruby will have a really good XML parser - speedy as a SAX and fully DOMinanting over the vagaries of namespaces etc.

Enclosed is an RSpec test that demonstrates usage.


Copy this code and paste it in your HTML
  1. require 'rexml/document'
  2.  
  3. class REXML::Element
  4.  
  5. public
  6. def lang
  7. if self.attributes['xml:lang']
  8. return self.attributes['xml:lang'].to_s
  9. elsif self.parent != nil
  10. return self.parent.lang
  11. else
  12. return nil
  13. end
  14. end
  15.  
  16. end
  17.  
  18. describe "XML library" do
  19. it "should handle xml:lang inheritance properly" do
  20. xmldoc = <<-EOF;
  21. <?xml version="1.0" ?>
  22. <foo xml:lang="en">
  23. <bar>Hello World!</bar>
  24. </foo>
  25. EOF
  26.  
  27. xml = REXML::Document.new(xmldoc)
  28. xml.elements[1].elements[1].lang.should == "en"
  29.  
  30. xmldoc2 = <<-EOF;
  31. <?xml version="1.0" ?>
  32. <foo>
  33. <bar>Hello World!</bar>
  34. </foo>
  35. EOF
  36.  
  37. xml2 = REXML::Document.new(xmldoc2)
  38. xml2.elements[1].elements[1].lang.should_not == "en"
  39. xml2.elements[1].elements[1].lang.should == nil
  40.  
  41. xmldoc3 = <<-EOF;
  42. <?xml version="1.0" ?>
  43. <foo>
  44. <bar xml:lang="en">Hello World!</bar>
  45. </foo>
  46. EOF
  47.  
  48. xml3 = REXML::Document.new(xmldoc3)
  49. xml3.elements[1].elements[1].lang.should == "en"
  50. end
  51. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.