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:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
I am also having such a requirement. When I used saxon namesapce as suggested I get unknown fuction error. Can you please help me on this. I am using Saxon B version.
declare namespace saxon="http://saxon.sf.net/";
let $x := <c>content</c>
return saxon:serialize($x, "")
Thanks
Senthur
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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.
Thank you so much. That was very helpful and met my needs. I was able to do it without post processing.
Thanks,
Steve
Hi
I am also having such a requirement. When I used saxon namesapce as suggested I get unknown fuction error. Can you please help me on this. I am using Saxon B version.
declare namespace saxon="http://saxon.sf.net/";
let $x := <c>content</c>
return saxon:serialize($x, "")
Thanks
Senthur