Project

Profile

Help

Generate CDATA with XQuery

Added by Anonymous over 17 years ago

Legacy ID: #4183095 Legacy Poster: Steve Wardell (okapi1253)

I am attempting to generate a CDATA section using XQuery with content within the CDATA section, but am not able to do this. What I am looking to return is something like: <b><![CDATA[<a></a>]]></b> Where <a></a> comes from an XPath value. However, I am not able to achieve this output. I have tried. Some things that I have tried: {"<![CDATA["}{ $pkg }{"]]>"} <![CDATA[{ $pkg }{"]]> However, these approaches do not generate the content that I expected as they either literally output { $pkg } or escape the > and < characters. Any assistance that can be provided would be appreciated. Thanks, Steve


Replies (2)

RE: Generate CDATA with XQuery - Added by Anonymous over 17 years ago

Legacy ID: #4183188 Legacy Poster: Michael Kay (mhkay)

CDATA sections are an artefact of serialized (lexical) XML, they have no representation in the XDM data model. This is because they are supposed to be used only as a convenient way to avoid escaping special characters, not as a way of conveying information. In your example <b><![CDATA[<a></a>]]></b> you say that <a></a> comes from an XPath value, but you don't say whether the value is a string of 7 characters, or an element node. The fact that you're putting it in CDATA suggests it's a string of 7 characters, but the fact that it looks suspiciously like XML markup suggests otherwise. If you put declare option saxon:output "cdata-section-elements=b"; in the prolog, then the content of every b element will be serialized as CDATA. However, if the content is an element node, this won't work unless you serialize the element node first, which you can do as <b>{saxon:serialize($pkg)}</b> Probably a better approach - if you really need to produce this output - is to do some non-XQuery postprocessing of the output. Generate <?cdata-start?> and <?cdata-end?> in the result as processing instructions, and then post-process the serialized-output with a text manipulation tool such as sed or Perl.

RE: Generate CDATA with XQuery - Added by Anonymous over 17 years ago

Legacy ID: #4188983 Legacy Poster: Steve Wardell (okapi1253)

Thank you so much. That was very helpful and met my needs. I was able to do it without post processing. Thanks, Steve

    (1-2/2)

    Please register to reply