【问题标题】:Ruby Nokogiri XML Parsing for NodeSetNodeSet 的 Ruby Nokogiri XML 解析
【发布时间】:2021-01-20 16:28:02
【问题描述】:

我在使用 Nokogiri 解析 XML 时遇到问题 Ruby 2.6.5。我检查了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> &amp; <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)

【问题讨论】:

    标签: ruby xml nokogiri


    【解决方案1】:

    我在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>
    
    

    【讨论】:

    • 好的,我应该尝试一下。我立即选择了xpath,因为它是正确的XML,并且没有css。但它确实有效。
    【解决方案2】:

    这是因为 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"
    

    【讨论】:

    • 完美。有没有关于这方面的 Nokogiri 笔记?我发现缺少文档。谢谢。
    • nokogiri.org 网站上的教程有一点关于使用命名空间进行搜索:nokogiri.org/tutorials/…
    • 我完全错过了。干杯
    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多