1

如何使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?

4

1 回答 1

1

我在搜索 hxt 的源代码时没有找到这样的选项,但是您始终可以mkCdata显式调用以构造 CDATA 文本节点:

import Text.XML.HXT.Core

hello :: ArrowXml a => a XmlTree XmlTree
hello =
  mkelem "texts" [] [constA "hello<br>world!" >>> mkCdata]

您可以定义一个类似于txt代码txt的函数:

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!"]
于 2016-04-21T10:55:36.650 回答