Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CDATA标记包装包含特殊字符的XML文本_Xml_Clojure_Xml Parsing - Fatal编程技术网

使用CDATA标记包装包含特殊字符的XML文本

使用CDATA标记包装包含特殊字符的XML文本,xml,clojure,xml-parsing,Xml,Clojure,Xml Parsing,在Clojure中,我如何遍历XML数据结构并用CDATA标记包装所有包含特殊字符的文本内容 例如,以下XML: <root> <child>no special characters</child> <child>special characters &amp;</child> <parent> <child>special characters &gt;</child

在Clojure中,我如何遍历XML数据结构并用CDATA标记包装所有包含特殊字符的文本内容

例如,以下XML:

<root>
  <child>no special characters</child>
  <child>special characters &amp;</child>
  <parent>
    <child>special characters &gt;</child>
  </parent>
</root>

没有特殊字符
特殊人物及;
特殊字符
应该成为

<root>
  <child>no special characters</child>
  <child><![CDATA[special characters &]]></child>
  <parent>
    <child><![CDATA[special characters >]]></child>
  </parent>
</root>

没有特殊字符
]]>

以下内容包装了包含CDATA中的
&
的文本节点。使用Clojure.data.xml 0.0.7在Clojure 1.5.1 REPL上测试:

(require '[clojure.data.xml :as xml] '[clojure.zip :as zip])

;; as in the question text:
(def test-xml
  "<root>
     <child>no special characters</child>
     <child>special characters &amp;</child>
     <parent>
       <child>special characters &gt;</child>
     </parent>
   </root>")

(def x (xml/parse-str test-xml))
(def z (zip/xml-zip x))

(defn contains-special-chars? [s]
  (.find (re-matcher #"[<>&]" s)))

(loop [z z]
  (if (zip/end? z)
    (-> z zip/root xml/emit-str)
    (let [n (zip/node z)]
      (if (string? n)
        (if (contains-special-chars? n)
          (recur (zip/edit z xml/->CData))
          (recur (zip/next z)))
        (recur (zip/next z))))))
(需要“[clojure.data.xml:as-xml]”[clojure.zip:as-zip])
;; 如问题案文所述:
(def)测试xml
"
没有特殊字符
特殊人物及;
特殊字符
")
(def x(xml/parse str test xml))
(def z(zip/xml-zip-x))
(defn包含特殊字符?[s]
(.find(重新匹配器#“[&]”s)))
(循环[z]
(如果(zip/end?z)
(->z zip/root-xml/emit-str)
(设[n(zip/node z)]
(如果(字符串?n)
(如果(包含特殊字符?n)
(重复(zip/editzxml/->CData))
(重复(zip/next z)))
(重复(zip/next z(()())))

以下内容包装了包含CDATA中的
&
的文本节点。使用Clojure.data.xml 0.0.7在Clojure 1.5.1 REPL上测试:

(require '[clojure.data.xml :as xml] '[clojure.zip :as zip])

;; as in the question text:
(def test-xml
  "<root>
     <child>no special characters</child>
     <child>special characters &amp;</child>
     <parent>
       <child>special characters &gt;</child>
     </parent>
   </root>")

(def x (xml/parse-str test-xml))
(def z (zip/xml-zip x))

(defn contains-special-chars? [s]
  (.find (re-matcher #"[<>&]" s)))

(loop [z z]
  (if (zip/end? z)
    (-> z zip/root xml/emit-str)
    (let [n (zip/node z)]
      (if (string? n)
        (if (contains-special-chars? n)
          (recur (zip/edit z xml/->CData))
          (recur (zip/next z)))
        (recur (zip/next z))))))
(需要“[clojure.data.xml:as-xml]”[clojure.zip:as-zip])
;; 如问题案文所述:
(def)测试xml
"
没有特殊字符
特殊人物及;
特殊字符
")
(def x(xml/parse str test xml))
(def z(zip/xml-zip-x))
(defn包含特殊字符?[s]
(.find(重新匹配器#“[&]”s)))
(循环[z]
(如果(zip/end?z)
(->z zip/root-xml/emit-str)
(设[n(zip/node z)]
(如果(字符串?n)
(如果(包含特殊字符?n)
(重复(zip/editzxml/->CData))
(重复(zip/next z)))
(重复(zip/next z(()())))