使用Haskell XHT库渲染CDATA

时间:2016-04-20 07:18:57

标签: xml haskell hxt

如何让HXT库输出CDATA?

例如,在此代码段中运行test将导致

<?xml version="1.0" encoding="UTF-8"?>
<texts>hello&lt;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?

1 个答案:

答案 0 :(得分:1)

我在查看hxt源代码时没有找到这样的选项,但是你总是可以显式调用filecontents = "test\r\n" filetype = "text\plain" 来构建一个CDATA文本节点:

mkCdata

您可以定义一个与import Text.XML.HXT.Core hello :: ArrowXml a => a XmlTree XmlTree hello = mkelem "texts" [] [constA "hello<br>world!" >>> mkCdata] 相似的函数,其方式与定义txt in the source的方式相同:

txt