I want to find a element like this.
xml1 = '<period>2017-02-10</period>'
or
xml2 = <<XML
<root xmlns:xbrli="http://www.w3.org/1999/xhtml">
<xbrli:period>2017-02-10</period>
</root>
XML
I can select the element by:
def period_from_xml(xml)
doc = Nokogiri::XML(xml)
period_element = if doc.namespaces.keys.include?('xmlns:xbrli')
doc.at_css("xbrli|period")
else
doc.at_css("period")
end
end
period_from_xml(xml1)
# => <period>2017-02-10</period>
period_from_xml(xml2)
# => <xbrli:period>2017-02-10</period>
I know Nokogiri::XML::Document#remove_namespaces!
, but I don't want to use it, because another place I need it.
Maybe duplicating the doc
and doc_without_namespaces
is good idea?
Is there a easy and simple way to handle this situation?