How can I parse XML using ruby for a duplicate tag? -


   <recipes>   <recipe name="mpeg4w480f20h360aac" mimetype="video/mp4" width="480" height="360" />   <recipe name="mp3" mimetype="audio/mpeg" />   <recipe name="j24" mimetype="image/jpeg" width="24" height="24" />   <recipe name="j64" mimetype="image/jpeg" width="64" height="64" />   <recipe name="j128" mimetype="image/jpeg" width="128" height="128" />   <recipe name="j88" mimetype="image/jpeg" height="88" />   <recipe name="j150" mimetype="image/jpeg" height="150" />   <recipe name="hqpivot" mimetype="image/jpeg" width="600" />    </recipes> 

i writing ruby script verify xml structure.

assert(!xpath.match(xml, "recipes/recipe[@name]").empty?, "structure of xml incorrect") 

how can verify 1 one?

using nokogiri:

require 'nokogiri'  f = file.open("test.xml") doc = nokogiri::xml(f)  tags = []  doc.xpath("//recipe/@name").each |elem| # name of recipes   tags << elem.value end  p tags # show tags p tags == tags.uniq # tags unique?  f.close 

this output (using sample):

["mpeg4w480f20h360aac", "mp3", "j24", "j64", "j128", "j88", "j150", "hqpivot"] true 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -