Nokogiri every node do Ruby

I have this xml:

<kapitel> <nummer V="1"/> <von_icd_code V="A00"/> <bis_icd_code V="B99"/> <bezeichnung V="Bestimmte infektiöse und parasitäre Krankheiten"/> <gruppen_liste> <gruppe> <von_icd_code V="A00"/> <bis_icd_code V="A09"/> <bezeichnung V="Infektiöse Darmkrankheiten"/> <diagnosen_liste> <diagnose> <icd_code V="A00.-"/> <bezeichnung V="Cholera"/> <abrechenbar V="n"/> <krankheit_in_mitteleuropa_sehr_selten V="j"/> <schlüsselnummer_mit_inhalt_belegt V="j"/> <infektionsschutzgesetz_meldepflicht V="j"/> <infektionsschutzgesetz_abrechnungsbesonderheit V="j"/> 

As you can see, my first node is kapitel . I would like to do something like kapitel .each do | f | so nokgiri extracts the nodes von_icd_code and bis_icd_code in the correct order. My code is:

  require 'rubygems' require 'nokogiri' require 'open-uri' @doc = Nokogiri::XML(File.open("icd.xml")) kapitel = @doc.css('kapitel') kapitel.each do |f| puts f.css('von_icd_code') puts f.css('bis_icd_code') end 

The problem is that nogiri does not increment "von_icd_code" and "bis_icd_code" on the right side, instead, it first lists all von_icd_code and then all "bis_icd_code". How can I extract the nodes on the right?

And in my release, I get:

 <von_icd_code V="A00"/> 

How can I get the contents of V in this case A00

Thanks!

+6
source share
3 answers

Since bis_icd_code follows every von_icd_code , the obvious choice is css + following neighboring selector:

 doc.css('von_icd_code').each do |icd| puts icd['V'] puts icd.at('+ bis_icd_code')['V'] end #=> A00 #=> B99 #=> A00 #=> A09 
+5
source

You can use the Nokogiri traverse method, which, well, recursively moves all the XML nodes.

Your example would look something like this:

 names = %w(von_icd_code bis_icd_code) @doc.traverse {|node| p node['V'] if names.include? node.name} 

And he is typing

 "A00" "B99" "A00" "A09" 

Nokogiri::Node lot of neat things that allow us to do really cool things with even the most complex XML files. For a short list, you can look at this hype .

Good luck

+5
source

Update

Sorry, this does not work with CSS selectors. Use XPath instead. As for your secondary question, in Nokogiri you can use the V attribute for node node using node['V'] . Like this

 kapitel = @doc.xpath('//kapitel') kapitel.each do |f| f.xpath('//von_icd_code | //bis_icd_code').each do |node| puts node['V'] end end 

Output

 A00 B99 A00 A09 

You can fix it without the extravagance of traverse by writing

 kapitel.each do |f| puts f.css('von_icd_code, bis_icd_code') end 
+2
source

Source: https://habr.com/ru/post/951449/


All Articles