使用 Haskell XHT 库呈现 CDATA
Rendering CDATA with Haskell XHT Library
如何使 HXT 库输出 CDATA?
例如,此代码段中的 运行 test
将导致
<?xml version="1.0" encoding="UTF-8"?>
<texts>hello<br>world!</texts>
import Text.XML.HXT.Core
hello :: ArrowXml a => a XmlTree XmlTree
hello =
mkelem "texts" [] [txt "hello<br>world!"]
test = runX $
root [] [hello]
>>>
writeDocument [withIndent yes] "somefile.xml"
但我需要它来渲染:
<?xml version="1.0" encoding="UTF-8"?>
<texts><![CDATA[hello<br>world!]]></texts>
能否HXT
自动检测是否需要CDATA?
我在搜索 hxt 的源代码时没有找到这样的选项,但是您总是可以显式调用 mkCdata
来构造 CDATA 文本节点:
import Text.XML.HXT.Core
hello :: ArrowXml a => a XmlTree XmlTree
hello =
mkelem "texts" [] [constA "hello<br>world!" >>> mkCdata]
并且您可以定义一个类似于 txt
的函数,其定义方式与 txt
的定义方式相同 in the source:
import qualified Text.XML.HXT.DOM.XmlNode as XN
txtCdata :: ArrowXml a => String -> a n XmlTree
-- XN.mkCdata :: XmlNode n => String -> n, XmlTree is an instance of XmlNode
-- constA :: Arrow a => c -> a b c, b is free
txtCdata = constA . XN.mkCdata
hello :: ArrowXml a => a XmlTree XmlTree
hello =
mkelem "texts" [] [txtCdata "hello<br>world!"]
如何使 HXT 库输出 CDATA?
例如,此代码段中的 运行 test
将导致
<?xml version="1.0" encoding="UTF-8"?>
<texts>hello<br>world!</texts>
import Text.XML.HXT.Core
hello :: ArrowXml a => a XmlTree XmlTree
hello =
mkelem "texts" [] [txt "hello<br>world!"]
test = runX $
root [] [hello]
>>>
writeDocument [withIndent yes] "somefile.xml"
但我需要它来渲染:
<?xml version="1.0" encoding="UTF-8"?>
<texts><![CDATA[hello<br>world!]]></texts>
能否HXT
自动检测是否需要CDATA?
我在搜索 hxt 的源代码时没有找到这样的选项,但是您总是可以显式调用 mkCdata
来构造 CDATA 文本节点:
import Text.XML.HXT.Core
hello :: ArrowXml a => a XmlTree XmlTree
hello =
mkelem "texts" [] [constA "hello<br>world!" >>> mkCdata]
并且您可以定义一个类似于 txt
的函数,其定义方式与 txt
的定义方式相同 in the source:
import qualified Text.XML.HXT.DOM.XmlNode as XN
txtCdata :: ArrowXml a => String -> a n XmlTree
-- XN.mkCdata :: XmlNode n => String -> n, XmlTree is an instance of XmlNode
-- constA :: Arrow a => c -> a b c, b is free
txtCdata = constA . XN.mkCdata
hello :: ArrowXml a => a XmlTree XmlTree
hello =
mkelem "texts" [] [txtCdata "hello<br>world!"]