Searching xml in Clojure

hbukmacherac

New Member
I have the following sample xml:\[code\]<data> <products> <product> <section>Red Section</section> <images> <image>img.jpg</image> <image>img2.jpg</image> </images> </product> <product> <section>Blue Section</section> <images> <image>img.jpg</image> <image>img3.jpg</image> </images> </product> <product> <section>Green Section</section> <images> <image>img.jpg</image> <image>img2.jpg</image> </images> </product> </products></data>\[/code\]I know how to parse it in Clojure\[code\](require '[clojure.xml :as xml])(def x (xml/parse 'location/of/that/xml'))\[/code\]This returns a nested map describing the xml\[code\]{:tag :data, :attrs nil, :content [ {:tag :products, :attrs nil, :content [ {:tag :product, :attrs nil, :content [] ..\[/code\]This structure can of course be traversed with standard Clojure functions, but it may turn out to be really verbose, especially if compared to, for instance, querying it with XPath. Is there any helper to traverse and search such structure? How can I, for example
  • get a list of all \[code\]<product>\[/code\]
  • get only the product whose \[code\]<images>\[/code\] tag contains an \[code\]<image>\[/code\] with text "img2.jpg"
  • get the product whose \[code\]section\[/code\] is "Red Section"
Thanks
 
Back
Top