Ruby Nokogiri XML 解析 NodeSet
Ruby Nokogiri XML Parsing for NodeSet
我在使用 Ruby 2.6.5
中的 Nokogiri
解析某些 XML
时遇到问题。我检查了 here 和其他帖子,但我似乎仍然无法获取 Nokogiri
位。我尝试了不同的节点,结果都是 thing 是 NilClass
.
require 'nokogiri'
xml_str = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">
<Document>
<description><![CDATA[powered by <a href="http://www.wordpress.org">WordPress</a> & <a href="https://www.mapsmarker.com">MapsMarker.com</a>]]></description>
<open>0</open>
<Style id="bar"><IconStyle><Icon><href>http://epic-curiousity.com/wp-content/uploads/leaflet-maps-marker-icons/bar.png</href></Icon></IconStyle></Style>
<name>Epic Curiousity</name>
<Placemark id="marker-35">
<styleUrl>#bar</styleUrl>
<name>Brasserie de Rochefort</name>
<TimeStamp><when>2014-06-13T07:06:01-08:00</when></TimeStamp>
<atom:author><atom:name>epiccuri</atom:name></atom:author>
<atom:link rel="related" href="http://epic-curiousity.com" />
<description><![CDATA[The Brasserie de Rochefort is located inside the Abbey of Notre-Dame de Saint-Rémy in Rochefort. They're a trappist brewery and produce three very-fine beers:<ul><li>Trappistes Rochefort 6 - Dubbel 7.5% ABV</li><li>Trappistes Rochefort 8 - Belgian Strong Dark Ale 9.2% ABV</li><li>Trappistes Rochefort 10 - Quadrupel (Quad) 11.30% ABV</li></ul>You can find the brewery's homepage here: <a href="http://www.trappistes-rochefort.com/">http://www.trappistes-rochefort.com/</a><br /><br />Their BeerAdvocate page is located here: <a href="http://www.beeradvocate.com/beer/profile/207/">http://www.beeradvocate.com/beer/profile/207/</a>]]></description>
<address><![CDATA[Brasserie Scaillet, Rue de la Griotte, Rochefort, Belgium]]></address>
<Point>
<coordinates>5.199692,50.175346</coordinates>
</Point>
</Placemark>
</Document>
</kml>
EOF
doc = Nokogiri::XML(xml_str)
puts doc.class # => Nokogiri::XML::Document
thing = doc.at_xpath("Document")
puts thing.class # ==> NilClass
有人知道为什么不将其识别为节点集吗?我也尝试过同样的结果:
doc = Nokogiri::XML.parse(xml_str)
我在 docs 上看到您可以使用 css
找到您想要的东西
puts doc.at_css("Document")
# show all node Document
puts doc.css("name")
# <name>Epic Curiousity</name>
# <name>Brasserie de Rochefort</name>
puts doc.css("Placemark name")
# <name>Brasserie de Rochefort</name>
puts doc.css("Document/name")
# <name>Epic Curiousity</name>
这是因为 xml 有命名空间,这些需要包含在 xpath 查询中:
document = doc.at_xpath('//xmlns:Document')
document.class
=> Nokogiri::XML::Element
document_name = doc.at_xpath('//xmlns:Document/xmlns:name')
document_name.class
=> Nokogiri::XML::Element
document_name.content
=> "Epic Curiousity"
要在 atom:name 中取名:
atom_name = doc.at_xpath('//atom:name')
atom_name.content
=> "Epic Curiousity"
我在使用 Ruby 2.6.5
中的 Nokogiri
解析某些 XML
时遇到问题。我检查了 here 和其他帖子,但我似乎仍然无法获取 Nokogiri
位。我尝试了不同的节点,结果都是 thing 是 NilClass
.
require 'nokogiri'
xml_str = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">
<Document>
<description><![CDATA[powered by <a href="http://www.wordpress.org">WordPress</a> & <a href="https://www.mapsmarker.com">MapsMarker.com</a>]]></description>
<open>0</open>
<Style id="bar"><IconStyle><Icon><href>http://epic-curiousity.com/wp-content/uploads/leaflet-maps-marker-icons/bar.png</href></Icon></IconStyle></Style>
<name>Epic Curiousity</name>
<Placemark id="marker-35">
<styleUrl>#bar</styleUrl>
<name>Brasserie de Rochefort</name>
<TimeStamp><when>2014-06-13T07:06:01-08:00</when></TimeStamp>
<atom:author><atom:name>epiccuri</atom:name></atom:author>
<atom:link rel="related" href="http://epic-curiousity.com" />
<description><![CDATA[The Brasserie de Rochefort is located inside the Abbey of Notre-Dame de Saint-Rémy in Rochefort. They're a trappist brewery and produce three very-fine beers:<ul><li>Trappistes Rochefort 6 - Dubbel 7.5% ABV</li><li>Trappistes Rochefort 8 - Belgian Strong Dark Ale 9.2% ABV</li><li>Trappistes Rochefort 10 - Quadrupel (Quad) 11.30% ABV</li></ul>You can find the brewery's homepage here: <a href="http://www.trappistes-rochefort.com/">http://www.trappistes-rochefort.com/</a><br /><br />Their BeerAdvocate page is located here: <a href="http://www.beeradvocate.com/beer/profile/207/">http://www.beeradvocate.com/beer/profile/207/</a>]]></description>
<address><![CDATA[Brasserie Scaillet, Rue de la Griotte, Rochefort, Belgium]]></address>
<Point>
<coordinates>5.199692,50.175346</coordinates>
</Point>
</Placemark>
</Document>
</kml>
EOF
doc = Nokogiri::XML(xml_str)
puts doc.class # => Nokogiri::XML::Document
thing = doc.at_xpath("Document")
puts thing.class # ==> NilClass
有人知道为什么不将其识别为节点集吗?我也尝试过同样的结果:
doc = Nokogiri::XML.parse(xml_str)
我在 docs 上看到您可以使用 css
找到您想要的东西
puts doc.at_css("Document")
# show all node Document
puts doc.css("name")
# <name>Epic Curiousity</name>
# <name>Brasserie de Rochefort</name>
puts doc.css("Placemark name")
# <name>Brasserie de Rochefort</name>
puts doc.css("Document/name")
# <name>Epic Curiousity</name>
这是因为 xml 有命名空间,这些需要包含在 xpath 查询中:
document = doc.at_xpath('//xmlns:Document')
document.class
=> Nokogiri::XML::Element
document_name = doc.at_xpath('//xmlns:Document/xmlns:name')
document_name.class
=> Nokogiri::XML::Element
document_name.content
=> "Epic Curiousity"
要在 atom:name 中取名:
atom_name = doc.at_xpath('//atom:name')
atom_name.content
=> "Epic Curiousity"