/ Published in: Ruby
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
require 'rexml/document' class REXML::Element public def lang if self.attributes['xml:lang'] return self.attributes['xml:lang'].to_s elsif self.parent != nil return self.parent.lang else return nil end end end describe "XML library" do it "should handle xml:lang inheritance properly" do xmldoc = <<-EOF; <?xml version="1.0" ?> <foo xml:lang="en"> <bar>Hello World!</bar> </foo> EOF xml = REXML::Document.new(xmldoc) xml.elements[1].elements[1].lang.should == "en" xmldoc2 = <<-EOF; <?xml version="1.0" ?> <foo> <bar>Hello World!</bar> </foo> EOF xml2 = REXML::Document.new(xmldoc2) xml2.elements[1].elements[1].lang.should_not == "en" xml2.elements[1].elements[1].lang.should == nil xmldoc3 = <<-EOF; <?xml version="1.0" ?> <foo> <bar xml:lang="en">Hello World!</bar> </foo> EOF xml3 = REXML::Document.new(xmldoc3) xml3.elements[1].elements[1].lang.should == "en" end end