On 07/24/2014 02:45 PM, Adrian O'Sullivan wrote:
I'm really struggling to understand how to parse XML in Clojure.
Here's my xml:
<javancss>
<function>
<name>java.complexity.HelloWorld.notcomplex()</name>
<ncss>2</ncss>
<ccn>1</ccn>
<javadocs>0</javadocs>
</function>
<function>
<name>java.complexity.HelloWorld.complex(int)</name>
<ncss>4</ncss>
<ccn>2</ccn>
<javadocs>0</javadocs>
</function>
... etc
I can do (xml-seq (xml/parse .. )) on this, or ( zip/xml-zip
(xml/parse ... ), I don't mind which.
My task is to extract all the function elements where the ncss value >
3, then sum the ccn values of the remaining elements. I simply cannot
figure out how to do this. All I can do is extract text of particular
elements, e.g.
(apply + (map read-string (zf/xml-> ncss-zip :functions :function :ccn
zf/text)))
If anyone can help out, it would be much appreciated.
--
Parsing XML without using XPath is (IMHO) a fool's errand...
Use the excellent clojure saxon library https://github.com/pjt/saxon
[clojure-saxon "0.9.4"]
(require '[saxon :as sax])
(def xml (sax/compile-xml "<javancss>
<function>
<name>java.complexity.HelloWorld.notcomplex()</name>
<ncss>2</ncss>
<ccn>1</ccn>
<javadocs>0</javadocs>
</function>
<function>
<name>java.complexity.HelloWorld.complex(int)</name>
<ncss>4</ncss>
<ccn>2</ccn>
<javadocs>0</javadocs>
</function> ..."))
(def sum-ncss (sax/compile-xquery "sum(//function[ncss<=3]/ncss")))
;;creates a function
(sum-ncss xml)
will return what you're looking for.
Hope this helps,
StanD.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.