Finding a node in xml using Nokogiri xpath (with xml namesapce)

I found that Nokogiri is pretty good at working with xml, but I met a special case

I am trying to search in node in an XML file, for example

<?xml version="1.0" encoding="utf-8" ?> <ConfigurationSection> <Configuration xmlns="clr-namespace:Newproject.Framework.Server.Store.Configuration;assembly=Newproject.Framework.Server" > <Configuration.Store>SqlServer</Configuration.Store> <Configuration.Engine>Staging</Configuration.Engine> </Configuration> </ConfigurationSection> 

When i do

 xml = File.new(webconfig,"r") doc = Nokogiri::XML(xml.read) nodes = doc.search("//Configuration.Store") xml.close 

I have empty nodes. Is something missing? I tried

  nodes = doc.search("//Configuration\.Store") 

still no luck.

Updated: I linked the whole XML file

Updated xml Again: My mistake, it has namaspace

+4
source share
2 answers

EDIT # 2: now solution includes #parse_with_namespace

In the Nokogiri :: XML :: Node documentation, you can find a number of Nokogiri methods related to namespaces.

 # encoding: UTF-8 require 'rspec' require 'nokogiri' XML = <<XML <?xml version="1.0" encoding="utf-8" ?> <ConfigurationSection> <Configuration xmlns="clr-namespace:Newproject.Framework.Server.Store.Configuration;assembly=Newproject.Framework.Server" > <Configuration.Store>SqlServer</Configuration.Store> <Configuration.Engine>Staging</Configuration.Engine> </Configuration> </ConfigurationSection> XML class ConfigParser def parse(xml) doc = Nokogiri::XML(xml).remove_namespaces! configuration = doc.at('/ConfigurationSection/Configuration') store = configuration.at("./Configuration.Store").text engine = configuration.at("./Configuration.Engine").text {store: store, engine: engine} end def parse_with_namespace(xml) doc = Nokogiri::XML(xml) configuration = doc.at('/ConfigurationSection/xmlns:Configuration', 'xmlns' => 'clr-namespace:Newproject.Framework.Server.Store.Configuration;assembly=Newproject.Framework.Server') store = configuration.at("./xmlns:Configuration.Store", 'xmlns' => 'clr-namespace:Newproject.Framework.Server.Store.Configuration;assembly=Newproject.Framework.Server').text engine = configuration.at("./xmlns:Configuration.Engine", 'xmlns' => 'clr-namespace:Newproject.Framework.Server.Store.Configuration;assembly=Newproject.Framework.Server').text {store: store, engine: engine} end end describe ConfigParser do before(:each) do @parsed = subject.parse XML @parsed_with_ns = subject.parse_with_namespace XML end it "should be able to parse the Configuration Store" do @parsed[:store].should eq "SqlServer" end it "should be able to parse the Configuration Engine" do @parsed[:engine].should eq "Staging" end it "should be able to parse the Configuration Store with namespace" do @parsed_with_ns[:store].should eq "SqlServer" end it "should be able to parse the Configuration Engine with namespace" do @parsed_with_ns[:engine].should eq "Staging" end end 
+5
source
 require 'nokogiri' XML = "<Configuration> <Configuration.Store>SqlServer</Configuration.Store> <Configuration.Engine>Staging</Configuration.Engine> </Configuration>" p Nokogiri::VERSION, Nokogiri.XML(XML).search('//Configuration.Store') #=> "1.5.0" #=> [#<Nokogiri::XML::Element:0x8103f0f8 name="Configuration.Store" children=[#<Nokogiri::XML::Text:0x81037524 "SqlServer">]>] p RUBY_DESCRIPTION #=> "ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]" 
0
source

Source: https://habr.com/ru/post/1389748/


All Articles