clj-xpath for xml with simple and nested tags



I have function (contents-only) that extracts contents from xml using clj-xpath library.



(ns example
(:use [clj-xpath.core]))

(def data-url
"http://ift.tt/1BaHRo1")

(defn xml-data [url] (slurp url))

(defn defxmldoc [url]
(xml->doc (xml-data url)))

(defn contents-only [url root-tag tags]
(vec(map(fn [item]
(into {}
(map (fn [tag]
[tag ($x:text (str "./" (name tag))item)])tags)))
(take 5 ($x root-tag (defxmldoc url))))))


The function call looks like this



(contents-only data-url "/search/events/event" [:title :url])


It works fine with not-nested tags, when I try to extract text from a nested tag ie.



<performers>
<performer>
<id>P0-001-000009049-1</id>
<url>...</url>
<name>Lindsey Buckingham</name>
<short_bio>Rock</short_bio>
<creator>TomAzoff</creator>
<linker>evdb</linker>
</performer>


Function call looks like this



(contents-only data-url "/search/events/event" [:title :url :name])


I get RuntimeException Error, more (or less) than 1 result (0) from xml({:children...) for xpath(./name) http://ift.tt/1FKQnZu (core.clj:26)


How to change my contents-only function, so I can pass a nested tag as well?


No comments:

Post a Comment