Re: clojure.data.zip Selecting a nested tag where tag shares same name as parent

2017-03-22 Thread Matching Socks

Does inserting clojure.zip/down between the two :thing's help?

If these examples are representative, then clojure.core/xml-seq could be 
simpler than zippers:

(def xmlstuff (first example1)) ;; you got a list from parse; get the root 
element

(->> xmlstuff xml-seq (filter #(= :thing (:tag %))) (mapcat :content) 
(filter string?))

;; ("Want this" "Select this")



Other techniques to consider:

- If capable selectors are important, consider Enlive.  It, too, starts 
with what you get from clojure.xml/parse, and it, too, uses zippers inside.

- If you like zippers, with a plain old loop you may use clojure.zip/next 
to traverse the tree and harvest the text nodes you need.  You could even 
look only at text nodes, and query the zipper for their parent...

- If you like zippers and xml->, the functions you can use in an xml-> path 
are not a closed set -- you may make a function that does what you want.


-- 
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.


clojure.data.zip Selecting a nested tag where tag shares same name as parent

2017-03-22 Thread Peter Schmiedeskamp
Hello,

I posted this question to StackOverflow, and one commenter suggested that 
this was a more appropriate place to ask this question 
(http://stackoverflow.com/questions/42940811/select-nested-xml-element-where-nested-tag-is-the-same-name-using-clojure-data-z)

I have a bit of XML that I'm wrangling using clojure.data.zip.xml. That XML 
has elements with the same name as nested elements:


  
Want this
...but not this
  
  
Select this
...again, not this
  


However, accessing the inner element seems to be beyond my reach with the 
xml-> function in clojure.data.zip.xml. If, however, the nested element 
does not share the same name as the parent, all works as expected:

(ns xmlzip.core
  (:require [clojure.xml :as xml]
[clojure.zip]
[clojure.data.zip.xml :as z])
  (:gen-class))

(defn parse [s]
  (clojure.xml/parse
   (java.io.ByteArrayInputStream. (.getBytes s

(def example1 (clojure.zip/xml-zip (parse "

  
Want this
...but not this
  
  
Select this
...again, not this
  

")))

(def example2 (clojure.zip/xml-zip (parse "

  
Want this
...but not this
  
  
Select this
...again, not this
  

")))


(z/xml-> example2 :thing :subthing z/text)
;; ("Want this" "Select this") ;hooray!

(z/xml-> example1 :thing :thing z/text)
;; ("Want this...but not this" "Select this...again, not this") ;boo, hiss.


I suspect that this may be an unaddressed case in clojure.data.zip.xml, but 
I am hoping that someone can show me the proper usage if I am simply 
misunderstanding how to use the library.

Kind regards,
Peter

-- 
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.