Hey,
I'm storing my XML namespaces for REXML as Hashes (for XPath.first [1]) like so:
RDF_NS = {"rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}
When I need to do an XPath lookup, these work great (especially as I
can merge() more than one together), but when I have to create new
Elements and add their namespaces, the Hash isn't what's expected[2]
(it needs two distinct args).
Is there a cooler way to get from a Hash to two args other than my
*Hash.to_a.flatten ?
require ‘rexml/document’
# => true
RDF_NS = {“rdf” => “http://www.w3.org/1999/02/22-rdf-syntax-ns#”}
# => {“rdf”=>“http://www.w3.org/1999/02/22-rdf-syntax-ns#”}
a = REXML::Element.new “rdf:RDF”
# => <rdf:RDF/>
a.add_namespace(*RDF_NS.to_a.flatten)
# => <rdf:RDF xmlns:rdf=‘http://www.w3.org/1999/02/22-rdf-syntax-ns#’/>
Thanks,
Keith
1. namespaces: If supplied, a Hash which defines a namespace mapping:
XPath.first( node, "a/x:b", { "x"=>"http://doofus" } )
http://www.germane-software.com/software/rexml/doc/classes/REXML/XPath.html#M000452
2. add_namespace( prefix, uri=nil )
Adds a namespace to this element.
prefix: the prefix string, or the namespace URI if uri is not supplied
uri: the namespace URI. May be nil, in which prefix is used as the URI
a.add_namespace("foo", "bar")
http://www.germane-software.com/software/rexml/doc/classes/REXML/Element.html#M000490