<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>
....
如何获取所有categories
和subcategories
数据?
我正在尝试类似的东西:
reader.xpath('//DataSet/diffgr:diffgram/alldata').each do |node|
但这给了我:
undefined method `xpath' for #<Nokogiri::XML::Reader:0x000001021d1750>
Nokogiri的Reader解析器不支持XPath。尝试改用 Nokogiri 的内存中文档解析器。
另一方面,要查询 xpath 命名空间,您需要提供命名空间映射,如下所示:
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
或者,您可以删除命名空间:
doc.remove_namespaces!
doc.xpath('//DataSet/diffgram/alldata').each { |node| }