1
<DataSet xmlns="http://www.atcomp.cz/webservices">
  <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="file_mame">...</xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <alldata xmlns="">
      <category diffgr:id="category1" msdata:rowOrder="0">
        <category_code>P.../category_code>
        <category_name>...</category_name>
        <subcategory diffgr:id="subcategory1" msdata:rowOrder="0">
          <category_code>...</category_code>
          <subcategory_code>...</subcategory_code>
          <subcategory_name>...</subcategory_name>
        </subcategory>
....

How can I obtain all categories and subcategories data?

I am trying something like:

reader.xpath('//DataSet/diffgr:diffgram/alldata').each do |node|

But this gives me:

undefined method `xpath' for #<Nokogiri::XML::Reader:0x000001021d1750>
0

1 Answer 1

4

Nokogiri's Reader parser does not support XPath. Try using Nokogiri's in-memory Document parser instead.

On another note, to query xpath namespaces, you need to provide a namespace mapping, like this:

doc = Nokogiri::XML(my_document_string_or_io)

namespaces = { 
  'default' => 'http://www.atcomp.cz/webservices', 
  'diffgr' => 'urn:schemas-microsoft-com:xml-diffgram-v1' 
}
doc.xpath('//default:DataSet/diffgr:diffgram/alldata', namespaces).each do |node|
  # ...
end

Or you can remove the namespaces:

doc.remove_namespaces!
doc.xpath('//DataSet/diffgram/alldata').each { |node|  }
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for you answer John, but unfortunately, the script don't go into the loop => so the parsing doesn't start
Also, I just tried to print out put doc and the result is only <?xml version="1.0"?>, which is very weird
it sounds like your my_document_string_or_io is empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.