Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

DataDirect XQuery, CDATA Sections and Namespace

Some time you may need to wrap text node in a CDATA section as part of your XQuery development. For example say you need to generate the following document

version="1.0" encoding="UTF-8"?>
<sf:Resource xmlns:sf="" contentID="PAYMENT">
            <total-due>bar</total-due>             <due-date>bar</due-date>         </payment-data> ]]></sf:Resource>

The following query makes use of a processor specific XQuery option called ddtek:serialize 

Notice the syntax to define an expanded QName with namespace is { {uri} local-name }  which is different from what is erroneously described in the XQuery Tips & Tricks 

The second takeaway is how the content inside sf:Resource needs to be escaped text to produce the required result

declare namespace sf="";
declare option ddtek:serialize "omit-xml-declaration=no,encoding=UTF-8,indent=yes,cdata-section-elements={{}Resource}";
declare variable $foo := "bar";
<sf:Resource contentID="PAYMENT">
       &lt;account-number&gt;{ $foo }&lt;/account-number&gt;
       &lt;total-due&gt;{ $foo }&lt;/total-due&gt;
       &lt;due-date&gt;{ $foo }&lt;/due-date&gt;

This post first appeared on Stylus Studio, please read the originial post: here

Share the post

DataDirect XQuery, CDATA Sections and Namespace


Subscribe to Stylus Studio

Get updates delivered right to your inbox!

Thank you for your subscription