Why does Clojure present XML documents as hash maps?

Can xml be a native clojure data type and allow simple type definitions

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

What is stopping 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 that supports namespaces.

+4
source share
2 answers

Your example does not work already when entering another inventor:

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

(Error: This is not a map.)

If you put both in one subcap, you cannot have more than one child of this type. This is important because the XML structure of the example is not designed properly. 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>

, , :

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

:

{: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"}]}]}

, , () . Clojure, .

+7

, , hiccup.

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

hiccup data.xml.

+5

Source: https://habr.com/ru/post/1529589/


All Articles