質問

Can xml be a native clojure datatype and allow simple definitions like

(def myxml <inventors><clojure>Rich Hickey</clojure></inventors>)

What prevents the current parsers from doing this

{:inventors {:clojure "Rich Hickey"}}

instead of this

{:tag :inventors, :attrs nil, :content [{:tag :clojure, :attrs nil, :content ["Rich Hickey"]}]}

A cursory search for similar representations in other lisps i see SXML which supports namespaces.

役に立ちましたか?

解決

Your example doesn't work already when you enter another inventor:

{:inventors
  {:clojure "Rich Hickey"}
  {:lisp "John McCarthy"}}

(Error: that is not a map.)

If you put both into one submap, you cannot have more than one child of a given type. That is important because the example XML structure is not properly designed. It should be something like:

<inventors>
  <inventor>
    <language>Clojure</language>
    <person>Rich Hickey</person>
  </inventor>
  <inventor>
    <language>Lisp</language>
    <person>John McCarthy</person>
  </inventor>
</inventors>

What you could do is use lists or vectors directly, instead of explicitly naming the components of a single tag:

[:inventors {}
  [:inventor {}
    [:language {} "Clojure"]
    [:person {} "Rich Hickey"]]
  [:inventor {}
    [:language {} "Lisp"]
    [:person {} "John McCarthy"]]]

That would look like this in the commonly used map structure:

{:tag :inventors
 :attrs nil
 :content [{:tag :inventor
            :attrs nil
            :content [{:tag :language
                       :attrs nil
                       :content "Clojure"}
                      {:tag :person
                       :attrs nil
                       :content "Rich Hickey"}]}
           {:tag :inventor
            :attrs nil
            :content [{:tag :language
                       :attrs nil
                       :content "Lisp"}
                      {:tag :person
                       :attrs nil
                       :content "John McCarthy"}]}]}

This might look like the vector proposal above is cleaner, but you would have to define accessor functions to (sensibly) work with those vectors. The maps already are their own accessor functions in Clojure, so you can directly and idiomatically work with them.

他のヒント

Substitute square braces for the curly ones and what you are talking about is hiccup format.

user=> (hiccup.core/html [:inventors [:clojure "Rich Hickey"]])
"<inventors><clojure>Rich Hickey</clojure></inventors>"

You can use hiccup-like style with data.xml.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top