最后几天,我正在寻找任何解决方案,使用Nokogiri获取多个节点,并使其符合祖先节点中的引用变量。
我需要什么: 实际上我正在收集“Segment”节点的所有“Id”。然后我想用“Segment”节点收集所有后续“资源”。为了收集“资源”,我想将“Id”设置为变量。
<CPL>
<SegmL>
<Segment>
<Id>UUID</Id> #UUID as a variable
<Name>name_01</Name>
<SeqL>
<ImageSequence>
<Id>UUID</Id>
<Track>UUID</Track>
<ResourceList>
<Resource> #depending on SegmentId
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
</Resource>
</ResourceList>
</ImageSequence>
<AudioSequence>
<Id>UUID</Id>
<Track>UUID</Track>
<ResourceList>
<Resource>
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
</Resource>
</ResourceList>
</AudioSequence>
</SequL>
</Segment>
<Segment>
<Id>UUIDa</Id>
<Name>name_02</Name>
<SequL>
<ImageSequence>
<Id>UUID</Id>
<Track>UUID</Track>
<ResourceList>
<Resource>
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
</Resource>
</ResourceList>
</ImageSequence>
<AudioSequence>
<Id>UUID</Id>
<Track>UUID</Track>
<ResourceList>
<Resource>
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
</Resource>
</ResourceList>
</AudioSequence>
</SequL>
</Segment>
</SegmL>
</CPL>
所有资源数据均使用A = Resource.css("A").text.gsub(/\n/,"")
#first each do
cpls.each_with_index do |(cpl_uuid, mycpl), index|
cpl_filename = mycpl
cpl_file = File.open("#{resource_uri}/#{cpl_filename}")
cpl = Nokogiri::XML( cpl_file ).remove_namespaces!
#get UUID for UUID checks
cpl_uuid = cpl.css("Id").first.text.gsub(/\n/,"")
cpl_root_edit_rate = cpl.css("EditRate").first.text.gsub(/\s+/, "\/")
#second each do
cpl.css("Segment").each do |s| # loop segment
cpl_segment_list_uuid = s.css("Id").first.text.gsub(/\n/,"") #uuid of segment list
#third each do
cpl.css("Resource").each do |f| #loop resources
cpl_A = f.css("A").text.gsub(/\n/,"") # uuid of A
cpl_B = f.css("B").text.gsub(/\n/,"") # uuid of B
end #third
end #second
end #first
我的表达式给了我存储在数组中的这些信息:
A = 48000.0
B = 240000.0
C = 0.0
D = 240000.0
Some functions to calculate an average on the resources.
puts all_arry
A = 5.0
B = 5.0
C = 5.0
D = 5.0
A = 5.0
B = 5.0
C = 5.0
D = 5.0
=8 values -> only 4 values existing for the exact loop (2 average values per Segment)
目前所有“SegmentId”收集所有“资源”
如何将每个Segment Id的后续资源完全分配为变量?
我曾经使用过这段代码,但循环是空的,因为有更多的节点在“Segment”的“Id”和每个“Resource”“A”,“B”......之后......:
if cpl.at("Segment/Id:contains(\"#{cpl_segment_list_uuid}\")")
cpl.css("Resource").each do |f|
#collecting resources here for each segmet
end
end
所有节点都没有属性,ID,类等。
愿你能解决我的问题。首先,我将在政治上感谢你的支持!
更新10/07/16
我还使用以下表达式为资源上的“each do”运行代码:
expression = "/SegmetList/Segment[Id>cpl_segment_list_uuid]"
cpl.xpath(expression).each do |f|
它运行“each do”,但我没有得到内部节点
cpl.css("Segment:contains(\"#{cpl_segment_list_uuid}\") > Resource").each do |f|
与之前相同
并且使用“if”条件,也是同样的问题:
if cpl.at("Segment/Id:contains(\"#{cpl_segment_list_uuid}\")").each do|f|
#some code
end
更新2016/18/10
实际上我获得了正确数量的资源(4),但仍然没有为每个细分分开。因此,每个细分中都有相同的四种资源。
为什么我没有获得所有资源的双倍数,是我在“Segment”-loop中创建数组。
这是现在的代码:
#first each do
cpls.each_with_index do |(cpl_uuid, mycpl), index|
cpl_filename = mycpl
cpl_file = File.open("#{resource_uri}/#{cpl_filename}")
cpl = Nokogiri::XML( cpl_file ).remove_namespaces!
#get UUID for UUID checks
cpl_uuid = cpl.css("Id").first.text.gsub(/\n/,"")
cpl_root_edit_rate = cpl.css("EditRate").first.text.gsub(/\s+/, "\/")
#second each do
cpl.css("Segment").each do |s| # loop segment
cpl_segment_list_uuid = s.css("Id").first.text.gsub(/\n/,"") #uuid of segment list
array_for_resource_data = Array.new
#third each do
s.css("Resource").each do |f| #loop resources #all resources
s.search('//A | //B').each do |f| #selecting only resources "A" and "B"
cpl_A = f.css("A").text.gsub(/\n/,"") # uuid of A
cpl_B = f.css("B").text.gsub(/\n/,"") # uuid of B
end #third
end #second
end #first
我希望我的更新能为您提供更多详情。非常感谢你的帮助和回答!
更新2016/31/10
段的双输出问题是固定的。现在我在段下的每个序列上还有一个循环:
cpl.css("Segment").each do |u|
segment_list_uuid = u.css("Id").first.text.gsub(/\n/,"")
sequence_list_uuid_arr = Array.new
u.xpath("//SequenceList[//*[starts-with(name(),'Sequence')]]").each do |s|
sequence_list_uuid = s.css("TrackId").first.text#.gsub(/\n/,"")
sequence_list_uuid_arr.push(cpl_sequence_list_uuid)
#following some resource nodes
s.css("Resource").each do |f|
asset_uuid = f.css("TrackFileId").text.gsub(/\n/,"")
resource_uuid = f.css("Id").text.gsub(/\n/,"")
edit_rate = f.css("EditRate").text.gsub(/\s+/, "\/")
#some more code
end #resource
end #sequence list
end #segment
现在我希望在每个独特序列下获得所有不同的“资源”。我必须列出所有不同的资源,并总结一些收集的值。
有没有办法在相同的“序列ID”下收集具有不同值(子节点)的每个资源?目前,我不知道任何解决方案....所以没有我能告诉你的代码,这些代码可以部分工作。
“资源”循环的each_with_index不起作用。
愿你有一些想法或任何方法来帮助我解决我的新问题吗?
答案 0 :(得分:0)
尝试
resource.search('.//A | .//B')
.//
将xpath查询锚定在当前元素而不是搜索整个文档。
实施例
elem = doc.search('ImageSequence').first
elem.search('//A') # returns all A in the whole document
elem.search('.//A') # returns all A inside element
答案 1 :(得分:0)
当拆解XML时,这是一个常见问题。编写类似于XML中数据布局方式的代码,从而允许重复类似数据的块。
例如:
require 'nokogiri'
cpl = Nokogiri::XML(<<EOT)
<CPL>
<SegmL>
<Segment>
<Id>UUID</Id> #UUID as a variable
<Name>name_01</Name>
<SeqL>
<ImageSequence>
<Id>UUID</Id>
<Track>UUID</Track>
<ResourceList>
<Resource> #depending on SegmentId
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
</Resource>
</ResourceList>
</ImageSequence>
<AudioSequence>
<Id>UUID</Id>
<Track>UUID</Track>
<ResourceList>
<Resource>
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
</Resource>
</ResourceList>
</AudioSequence>
</SequL>
</Segment>
</SegmL>
</CPL>
EOT
首先找到包含要迭代的数据的节点,然后开始下降到该数据:
data = cpl.search('Segment').each_with_object([]) { |segment, ary|
hash = {}
hash[:id] = segment.at('Id').text
hash[:name] = segment.at('Name').text
image_sequence = segment.at('ImageSequence')
image_sequence_h = {}
image_sequence_h[:id] = image_sequence.at('Id').text
image_sequence_h[:track] = image_sequence.at('Track').text
image_resources_h = {
a: image_sequence.at('A').text,
b: image_sequence.at('B').text,
c: image_sequence.at('C').text,
d: image_sequence.at('D').text,
}
audio_sequence = segment.at('AudioSequence')
audio_sequence_h = {}
audio_sequence_h[:id] = audio_sequence.at('Id').text
audio_sequence_h[:track] = audio_sequence.at('Track').text
audio_resources_h = {
a: audio_sequence.at('A').text,
b: audio_sequence.at('B').text,
c: audio_sequence.at('C').text,
d: audio_sequence.at('D').text,
}
image_sequence_h[:resources] = image_resources_h
audio_sequence_h[:resources] = audio_resources_h
hash[:image_sequence] = image_sequence_h
hash[:audio_sequence] = audio_sequence_h
ary << hash
}
这比我通常写的还要冗长,因为我希望步骤更清楚。
最终结果是一个哈希数组:
# => [{:id=>"UUID",
# :name=>"name_01",
# :image_sequence=>
# {:id=>"UUID",
# :track=>"UUID",
# :resources=>{:a=>"aaa", :b=>"bbb", :c=>"ccc", :d=>"ddd"}},
# :audio_sequence=>
# {:id=>"UUID",
# :track=>"UUID",
# :resources=>{:a=>"aaa", :b=>"bbb", :c=>"ccc", :d=>"ddd"}}}]
然后很容易遍历数组并访问单个数据块或数据的单个元素:
data[0][:image_sequence][:id] # => "UUID"
data[0][:audio_sequence][:resources][:d] # => "ddd"