I have two elements, item
and date
:
我有两个元素,项目和日期:
-
item
has_many dates - 项has_many日期
-
date
belongs_to item - 日期belongs_to项
I have a file with XML tree like this:
我有这样一个XML树文件:
<content>
<item_1>
<title>
<description>
<date_1>
<date></date>
<count></count>
</date_1>
<date_2>
<date></date>
<count></count>
</date_2>
</item_1>
<item_2>
<title>
<description>
<date_1>
<date></date>
<count></count>
</date_1>
<date_2>
<date></date>
<count></count>
</date_2>
</item_2>
</content>
I use Nokogiri for parsing data from item
and date
. I wrote a half of the script, which creates elements:
我使用Nokogiri解析项目和日期的数据。我写了剧本的一半,创造了元素:
doc.xpath("//content/*").each do |item|
Item.create!(
title: item.xpath("title").text,
description: item.xpath("description").text)
end
Now I need find and build all dates of the items (element date
), but can't understand how to parse tags like <data_1>
, <data_2>
, <data_3>
etc. What I was trying
现在我需要找到并构建项目的所有日期(元素日期),但是不理解如何解析标签,比如
tour.xpath("//*/data_*").each do |date|
puts date
end
or
或
tour.xpath{ |i| "//*/data_#{i}" }.each do |date|
puts date
end
but it doesn't work or I get an error.
但它不管用,否则我就会出错。
2 个解决方案
#1
5
The XPath for selecting all date
elements in your document, regardless of their heritage, is simply:
选择文档中所有日期元素的XPath,不管它们的传统是什么,就是:
//date
The XPath for selecting all elements whose name starts with "date_" is:
选择名称以“date_”开头的所有元素的XPath是:
//*[starts-with(local-name(), 'date_')]
See starts-with()
and local-name()
.
看到始于()和本地名称()。
#2
1
Try this:
试试这个:
doc.xpath("//content/*").each do |item|
...
item.xpath("./*[starts-with(local-name(), 'date_')]").each do |d|
...
end
end
(snipped edited according to @kjhughes suggestion)
(根据@kjhughes的建议进行剪辑)
#1
5
The XPath for selecting all date
elements in your document, regardless of their heritage, is simply:
选择文档中所有日期元素的XPath,不管它们的传统是什么,就是:
//date
The XPath for selecting all elements whose name starts with "date_" is:
选择名称以“date_”开头的所有元素的XPath是:
//*[starts-with(local-name(), 'date_')]
See starts-with()
and local-name()
.
看到始于()和本地名称()。
#2
1
Try this:
试试这个:
doc.xpath("//content/*").each do |item|
...
item.xpath("./*[starts-with(local-name(), 'date_')]").each do |d|
...
end
end
(snipped edited according to @kjhughes suggestion)
(根据@kjhughes的建议进行剪辑)