4 Replies Latest reply on Nov 10, 2010 12:20 PM by 755655

    CDATA and XML structure


      I am trying to but an entire XML structure into a CDATA-tag. But have not been successful.
      Im using Xquery and trying to somthing like this:

      I have an variable $example which contains a XML structure. For example:

      $example = <elementA><A1>valueA1</A1><A2><A2a>text2a</A2a><A2b>text2b</A2b></A2><A3>valueA3</A3></elementA>

      I want to something like this <![CDATA[$example]]> to be expanded to <![CDATA[<elementA><A1>valueA1</A1><A2><A2a>text2a</A2a><A2b>text2b</A2b></A2><A3>valueA3</A3></elementA>]]>

      I have tried in many ways (eg by doing concat("<![CDATA[", $example, "]]>") ) and ended up with a CDATA tag that contains only the value of the elements in the tags of A1, A2 and A3. for example the CDATA that is created looks something like this:
      instead of

      I would really be grateful for any suggestions or tips and pointers on how to solve this.

      Best Regards

      Edited by: NinibEDB on 2010-nov-09 17:35
        • 1. Re: CDATA and XML structure

          Which XQuery processor are you using?

          According to the W3C specifications, the output of CDATA section is part of the serialization process, not the XQuery evaluation. So it's up to the implementor to provide the necessary options.
          The option we need here is the "cdata-section-elements" parameter :


          For example, using the Saxon XQuery processor :
          declare option saxon:output "omit-xml-declaration=yes";
          declare option saxon:output "cdata-section-elements=test";
          let $example := "<elementA><A1>valueA1</A1><A2><A2a>text2a</A2a><A2b>text2b</A2b></A2><A3>valueA3</A3></elementA>"
          return <test>{$example}</test>
          which gives :
          1 person found this helpful
          • 2. Re: CDATA and XML structure

            Thanks for your answer.

            Im using BEA Aqualogic, so i think its the xquery processor that comes with that (BEA streaming xquery processor) if I'm not mistaken.

            So "declare option saxon:output" is not working in our processor. I need to find the BEAs syntax. Have tried goggling it without any success.

            • 3. Re: CDATA and XML structure
              You need to use fn-bea:serialize() function.
              Something like this, if you have an XML in $input then:
              (i.e.) your $input contains an XML like <elementA><A1>valueA1</A1><A2><A2a>text2a</A2a><A2b>text2b</A2b></A2><A3>valueA3</A3></elementA> then


              this will result in an output like following:


              If you view this in an output window of ALSB it will show the XML content as a serialized string with escape characters, but when transmitted to another system or stored as file it is actually CDATA section.
              • 4. Re: CDATA and XML structure
                It worked, THANK YOU SO MUCH, both of you for your help and for taking time and helping me.
                Really appreciate it.

                Best regards

                Edited by: NinibEDB on 2010-nov-10 13:19