开发者

eXist XQuery wrap variable in CDATA

I was wondering if it is possible to wrap the contents开发者_开发技巧 of a variable (that might contain messy html) into a cdata section.

I am using XQuery with eXist and I just can't seem to get it working.

I tried

<![CDATA[ $data ]]>
<![CDATA[ {$data} ]]>

In both cases the variable is not replaced by its contents, but remains $data and {$data} respectively.

I also tried using concat and other string functions, but these resulted in <![CDATA[ becoming &lt;![CDATA[.

The $data contains http get/post data from an html wysiwyg editor.

xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";

let $data := request:get-parameter("content" , "")
return <![CDATA[ {$data} ]]>

Does anyone now how it should be done? Thanks in advance.


@Alejandro is right. See Priscilla Walmsley's XQuery book pp. 280-281. "All of the text in a CDATA section is taken literally; it is not possible to include enclosed expressions in a CDATA section."

If you want to deal with "messy HTML" by fixing malformed bits, you may wish to check out the NekoHTML-powered util:parse-html() function. Pass the messy HTML string, and you'll get well-formed XML node on the other end.


If you want to encode some well-formed but arbitrary HTML as a string, for example to include in a KML description, use util:serialise()

eg.

xquery version "1.0";
declare namespace util="http://exist-db.org/xquery/util";

let $data := <div><h1>stuff</h1><a href="url"> <img src="image"/></a></div>
return
   util:serialize($data,"method=xml"))

The effect is the same as if the XML were enclosed in CDATA, with the reserved XML characters encoded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜