Clojure XML - is there a better way?

2014-07-22 Thread Adrian O'Sullivan
Hi! 

I struggled to use clojure.data.zip.xml in the absence of a root node.  My 
XML looks like this:



http://pmd.sourceforge.net/pmd-5.1.2/rules/java/codesize.html#TooManyMethods";
 
priority="3">
This class has too many methods, consider refactoring it.



http://pmd.sourceforge.net/pmd-5.1.2/rules/java/codesize.html#TooManyMethods";
 
priority="3">
This class has too many methods, consider refactoring it.

http://pmd.sourceforge.net/pmd-5.1.2/rules/java/codesize.html#ExcessiveMethodLength";
 
priority="3">
Avoid really long methods.



... more ...

I want to extract ExcessiveMethodLength violations, and then calculate the 
difference between the beginline and endline attributes.  Here's what I 
came up with:

(for [x pmd-seq :when (and 
(= :violation (:tag x)) 
(= "ExcessiveMethodLength" (:rule (:attrs x))) )]  
   (- (read-string (:endline (:attrs x))) (read-string (:beginline (:attrs 
x)))  ))


It does the job, but I feel that zip xml would have been better, but I 
could not figure out how to filter the data.

Any insights appreciated.

Thanks
Adrian


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Clojure XML - is there a better way?

2014-07-22 Thread Adrian O'Sullivan
Yes good point, makes me feel the solution is maybe ok.  I appreciate the 
feedback, thanks.

On Tuesday, July 22, 2014 4:55:53 PM UTC-4, Mike Fikes wrote:
>
> My only gut reaction is that, without a root node, it doesn't seem that 
> you have "XML". (It is certainly not a well formed document, and I would 
> suspect lots of XML tools/libraries would have difficulty coping with the 
> example you provided.)
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


More Clojure XML challenges

2014-07-24 Thread Adrian O'Sullivan
I'm really struggling to understand how to parse XML in Clojure.

Here's my xml:


   
  java.complexity.HelloWorld.notcomplex()
  2
  1
  0


  java.complexity.HelloWorld.complex(int)
  4
  2
  0
 
... 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.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: More Clojure XML challenges

2014-07-24 Thread Adrian O'Sullivan
Great, thanks for the feedback.  Will try it tomorrow.

On Thursday, July 24, 2014 7:22:35 PM UTC-4, Stan wrote:
>
>
> 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: 
> > 
> >  
> > 
> > java.complexity.HelloWorld.notcomplex() 
> >   2 
> >   1 
> >   0 
> >  
> >  
> > java.complexity.HelloWorld.complex(int) 
> >   4 
> >   2 
> >   0 
> >  
> > ... 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 " 
>  
> java.complexity.HelloWorld.notcomplex() 
>2 
>1 
>0 
>   
>   
> java.complexity.HelloWorld.complex(int) 
>4 
>2 
>0 
>   ...")) 
>
> (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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.