使用Clojure在Clojure中往返xml。xml解析和clojure.xml /排放

时间:2021-03-20 22:52:06

Below roundtrip produces invaild xml as the result is not escaped correctly, i.e. the attribute values contain ' instead of apos;. Am I doing somthing wrong or is this a bug?

以下是往返生成invaild xml,因为结果没有正确转义,即属性值包含“而不是apos;我做错了什么事,还是这是个错误?

(ns xml-test
  (:require [clojure.xml :as xml])
  (:require [clojure.zip :as zip]))

(def test-xml "<?xml version="1.0" encoding="UTF-8"?> <main> <item attr='&apos;test&apos;'> </item> </main>")

(def s (ByteArrayInputStream. (.getBytes test-xml "UTF-8")))

(xml/emit (zip/root (zip/xml-zip (clojure.xml/parse s))))

output:

<?xml version='1.0' encoding='UTF-8'?>
<main>
<item attr=''test''/>
</main>
nil

1 个解决方案

#1


8  

I've checked the source quickly and clojure.xml/emit-element (which gets called by clojure.xml/emit) makes no effort whatever to encode any characters as XML entities; in fact, it lets attribute values straight through. I guess this means clojure.xml is quite limited in its usability; you should use clojure.contrib.lazy-xml instead. My apologies for not mentioning it in the answer to your first question on XML emitting, I didn't realise stuff like this would happen.

我已经快速地检查了源文件。xml/emit元素(由clojure.xml/emit调用)不需要任何方法来将任何字符编码为xml实体;事实上,它让属性值直接通过。我想这意味着clojure。xml的可用性非常有限;您应该使用clojure.contrib。lazy-xml代替。我很抱歉没有在回答第一个关于XML的问题时提到它,我没有意识到这样的事情会发生。

With clojure.contrib.lazy-xml, you can do the following:

clojure.contrib。惰性xml,您可以执行以下操作:

user> (lazy-xml/emit
       (lazy-xml/parse-trim
        (java.io.StringReader. "<foo bar=\"&apos;&quot;&quot;&apos;\"/>")))
<?xml version="1.0" encoding="UTF-8"?><foo bar="'&quot;&quot;'"/>

If you really wanted to use clojure.xml, you'd have to pass on clojure.xml/emit and use an XML producer of your choice instead. Well, actually, you can use clojure.xml/parse, mangle the result, then pass it to clojure.contrib.lazy-xml/emit; the structure of the Clojure representation of the XML is the same with both libraries, but only the latter does proper emitting.

如果你真的想使用clojure。xml,你必须通过clojure。xml/emit并使用您选择的xml生成器。实际上,你可以用clojure。xml/parse,对结果进行分解,然后将其传递给clojuri .致歉。XML的Clojure表示的结构对于这两个库都是相同的,但是只有后者才进行适当的发送。

#1


8  

I've checked the source quickly and clojure.xml/emit-element (which gets called by clojure.xml/emit) makes no effort whatever to encode any characters as XML entities; in fact, it lets attribute values straight through. I guess this means clojure.xml is quite limited in its usability; you should use clojure.contrib.lazy-xml instead. My apologies for not mentioning it in the answer to your first question on XML emitting, I didn't realise stuff like this would happen.

我已经快速地检查了源文件。xml/emit元素(由clojure.xml/emit调用)不需要任何方法来将任何字符编码为xml实体;事实上,它让属性值直接通过。我想这意味着clojure。xml的可用性非常有限;您应该使用clojure.contrib。lazy-xml代替。我很抱歉没有在回答第一个关于XML的问题时提到它,我没有意识到这样的事情会发生。

With clojure.contrib.lazy-xml, you can do the following:

clojure.contrib。惰性xml,您可以执行以下操作:

user> (lazy-xml/emit
       (lazy-xml/parse-trim
        (java.io.StringReader. "<foo bar=\"&apos;&quot;&quot;&apos;\"/>")))
<?xml version="1.0" encoding="UTF-8"?><foo bar="'&quot;&quot;'"/>

If you really wanted to use clojure.xml, you'd have to pass on clojure.xml/emit and use an XML producer of your choice instead. Well, actually, you can use clojure.xml/parse, mangle the result, then pass it to clojure.contrib.lazy-xml/emit; the structure of the Clojure representation of the XML is the same with both libraries, but only the latter does proper emitting.

如果你真的想使用clojure。xml,你必须通过clojure。xml/emit并使用您选择的xml生成器。实际上,你可以用clojure。xml/parse,对结果进行分解,然后将其传递给clojuri .致歉。XML的Clojure表示的结构对于这两个库都是相同的,但是只有后者才进行适当的发送。