遍历 XML 文件的元素以查看它们是否包含数组中的任何值?
Loop through elements of XML file to see if they include any value within an array?
我已经阅读了大量的问题和解决方案以确定是否已经在其他地方回答了这个问题,但似乎 none 我发现的东西正是我想要得到的。
我有一个 XML 文档,其中包含数百个文本条目,每个条目还列出一个 URL。每个 URL 都是一个字符串(在标签内),以唯一的 4 位数字结尾。 XML 文件的基本格式如下:
<entry>
[other content]
<id>http://www.URL.com/blahblahblah-1234</id>
[other content]
</entry>
我只想从数字列表中挑出末尾有特定数字的 URL。我将所有数字放在一个数组中,并将值设置为字符串 (numbers = ["1234", "8649", etc.]
)。我一直在为脚本的其他部分使用 nokogiri,当我只查找特定字符串时,我只使用 include?
,效果很好。但是,当我在 "numbers" 数组中有数百个字符串时,我不确定如何自动执行此操作。这基本上是我逻辑上需要发生的事情:
id = nokodoc.css("id")
id.each { |id|
hyperlink = id.text
if hyperlink.include?(numbers)
puts "yes!"
else
puts "no :("
end
}
显然这是行不通的,因为 include?
需要一个字符串,而我传递的是整个数组。 (例如,如果我执行 include?(numbers[0])
,它会起作用。)我已经用 any?
尝试过,但在这种情况下似乎不起作用。
是否有一个我不知道的 Ruby 方法,它可以告诉我数组中 任何 值是否存在于 任何我正在循环的节点?如果有任何问题需要澄清,请告诉我——提出正确的问题通常是最困难的部分!
编辑: 作为旁注,最终我想删除所有与 not 结尾的链接相对应的条目数组中的数字之一,即
if hyperlink.include? (any number from the array)
puts "this one is good"
else
id.parent.remove
所以我会以某种方式需要最终产品来保持 nokogiri 的可解析性。
提前非常感谢您的任何见解!
你可以这样做:
numbers = ['1234', '8649', ..]
urls = nokodoc.css('id').map(&:text)
urls = urls.select { |url| numbers.any? { |n| url.include? n } }
但是效率不高。如果您知道模式 - 提取数字,然后检查它是否在数组中。例如,如果它始终是最后 4 位数字:
numbers = ['1234', '8649', ..]
urls = nokodoc.css('id').map(&:text)
urls = urls.select { |url| numbers.include? url[-4..-1] }
更新
对于问题的修改:
numbers = ['1234', '8649', ..]
nodes = nokodoc.css('id')
nodes.each do |node|
url = node.text
if numbers.any? { |n| url.include? n }
puts 'this one is good'
else
node.parent.remove
end
end
我已经阅读了大量的问题和解决方案以确定是否已经在其他地方回答了这个问题,但似乎 none 我发现的东西正是我想要得到的。
我有一个 XML 文档,其中包含数百个文本条目,每个条目还列出一个 URL。每个 URL 都是一个字符串(在标签内),以唯一的 4 位数字结尾。 XML 文件的基本格式如下:
<entry>
[other content]
<id>http://www.URL.com/blahblahblah-1234</id>
[other content]
</entry>
我只想从数字列表中挑出末尾有特定数字的 URL。我将所有数字放在一个数组中,并将值设置为字符串 (numbers = ["1234", "8649", etc.]
)。我一直在为脚本的其他部分使用 nokogiri,当我只查找特定字符串时,我只使用 include?
,效果很好。但是,当我在 "numbers" 数组中有数百个字符串时,我不确定如何自动执行此操作。这基本上是我逻辑上需要发生的事情:
id = nokodoc.css("id")
id.each { |id|
hyperlink = id.text
if hyperlink.include?(numbers)
puts "yes!"
else
puts "no :("
end
}
显然这是行不通的,因为 include?
需要一个字符串,而我传递的是整个数组。 (例如,如果我执行 include?(numbers[0])
,它会起作用。)我已经用 any?
尝试过,但在这种情况下似乎不起作用。
是否有一个我不知道的 Ruby 方法,它可以告诉我数组中 任何 值是否存在于 任何我正在循环的节点?如果有任何问题需要澄清,请告诉我——提出正确的问题通常是最困难的部分!
编辑: 作为旁注,最终我想删除所有与 not 结尾的链接相对应的条目数组中的数字之一,即
if hyperlink.include? (any number from the array)
puts "this one is good"
else
id.parent.remove
所以我会以某种方式需要最终产品来保持 nokogiri 的可解析性。
提前非常感谢您的任何见解!
你可以这样做:
numbers = ['1234', '8649', ..]
urls = nokodoc.css('id').map(&:text)
urls = urls.select { |url| numbers.any? { |n| url.include? n } }
但是效率不高。如果您知道模式 - 提取数字,然后检查它是否在数组中。例如,如果它始终是最后 4 位数字:
numbers = ['1234', '8649', ..]
urls = nokodoc.css('id').map(&:text)
urls = urls.select { |url| numbers.include? url[-4..-1] }
更新
对于问题的修改:
numbers = ['1234', '8649', ..]
nodes = nokodoc.css('id')
nodes.each do |node|
url = node.text
if numbers.any? { |n| url.include? n }
puts 'this one is good'
else
node.parent.remove
end
end