Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping Arbitrary XML within XML

I need to embed arbitrary (syntactically valid) XML documents within a wrapper XML document. The embedded documents are to be regarded as mere text, they do not need to be parseable when parsing the wrapper document.

I know about the "CDATA trick", but I can't use that if the inner XML document itself contains a CDATA segment, and I need to be able to embed any valid XML document. Any advice on accomplishing this--or working around the CDATA limitation--would be appreciated.

like image 811
Marc C Avatar asked Apr 22 '09 13:04

Marc C


4 Answers

You need to properly escape the text. You don't say what language you're using, but generally: you build a DOM, create a Text node that contains your "inner" XML, and then serialize that DOM. The serializer will handle escaping for you.

The key point here is use a serializer to produce your output. Don't simply write strings, because you're all but guaranteed to produce something that's not well-formed XML.

like image 111
kdgregory Avatar answered Oct 06 '22 06:10

kdgregory


You can do this by simply adding the document (without its <?xml declaration) as a child tom some parent. SOAP is doing this - it has a <Body> element that can contain whatever xml message one wants to send.

SOAP defines the XSD this way:

<xs:element name="Body" type="tns:Body" />
  <xs:complexType name="Body">
    <xs:sequence>
      <xs:any namespace="##any" minOccurs="0" 
          maxOccurs="unbounded" processContents="lax" />
    </xs:sequence>
    <xs:anyAttribute namespace="##any" processContents="lax">
    </xs:anyAttribute>
  </xs:complexType>
like image 28
Bozho Avatar answered Oct 06 '22 04:10

Bozho


When you escape the ending angular bracket of the inner CDATA, most XML parsers will not complain about the well-formedness of your XML. Using this "workaround", you should be able to nest multiple CDATA sections.

Something like:

<?xml version="1.0"?>
<SomeData>
<![CDATA[
<SomeMoreData>
<![CDATA[
yeah, this trick rocks! ...
]]&gt;
</SomeMoreData>
]]>
</SomeData>

Note that the inner CDATA has its ending ">" escaped as &gt;.

like image 26
Cerebrus Avatar answered Oct 06 '22 04:10

Cerebrus


Consider using XInclude instead of trying to embed an XML document inside another. The XInclude parse="text" attribute will force the XML to be treated as text, not markup.

like image 42
Dour High Arch Avatar answered Oct 06 '22 04:10

Dour High Arch