如何获取tag.contents
我有这个 XML:
<record>
<f id="27">John Smith</f>
<f id="28"/>
</record>
并用 Nokogiri 解析它:
# I get the record from the whole document
...
fields = record.xpath("f")
for field in fields
puts field.content
end
它返回这个:
John Smith
\n 28 \n
这是不正确的。第二个 field
标签内没有任何内容,它应该返回一个空值。正确的?
顺便说一句,同样的事情也发生在 LibXML 上。
这是实际代码:
xml = Nokogiri::XML("<?xml version="1.0" ?><records><record><f id="27">John Smith</f><f id="38"/></record></records>")
records = xml.xpath("//record")
records.map{|record|
fields = record.xpath("f")
fields.to_enum(:each_with_index).collect{|field,index|
[field.content, index]
}
}
I have this XML:
<record>
<f id="27">John Smith</f>
<f id="28"/>
</record>
and parse it with Nokogiri this way:
# I get the record from the whole document
...
fields = record.xpath("f")
for field in fields
puts field.content
end
which returns this:
John Smith
\n 28 \n
This is incorrect. The second field
tag does not have anything inside the tag, it should return an empty value. Right?
By the way, the same thing happens with LibXML.
This is the Actual code:
xml = Nokogiri::XML("<?xml version="1.0" ?><records><record><f id="27">John Smith</f><f id="38"/></record></records>")
records = xml.xpath("//record")
records.map{|record|
fields = record.xpath("f")
fields.to_enum(:each_with_index).collect{|field,index|
[field.content, index]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我来回答这个问题。该标签中可能包含您可能错过的其他标签。
I'll answer the question. The tag probably contains other tags in it that you might've missed.
您的 xpath 访问器是错误的:
Nokogiri 始终返回带有
xpath
、css
和search
方法的 NodeSet,以及at< 的 Node /code> 及其别名。将 NodeSet 视为数组。
Your xpath accessor is wrong:
Nokogiri always returns a NodeSet with the
xpath
,css
andsearch
methods, and a Node forat
and its aliases. Treat the NodeSet as an array.