-1

We have xml storage format such as

<record> data </record>

We have hit a use case where data itself can be xml. How can xml data be included inside another xml element such as

<record> 
Jimm
  • 7,173
  • 14
  • 59
  • 106

3 Answers3

9

It is perfectly valid for an XML element to contain XML like the following:

<record><record>test</record></record>

If this is not what you need, please clarify further in your question. As a last resort you may consider using CData Elements.

<record>
    <![CDATA[<someotherbadlyformedxml>data<br></someotherbadlyformedxml>]]>
</record>

But there really should be no reason to do so if all of your data is valid XML.

Chris Summers
  • 10,133
  • 1
  • 19
  • 46
  • Chris, What if data contains element. Then the output would look like abc. I guess if namespaces are different, then things should work. – Jimm May 08 '12 at 21:10
  • I am not sure what the problem is here - The XML is still valid (even without different namespaces). Are you having problems selecting the data? What is the challenge you are facing? – Chris Summers May 08 '12 at 21:15
  • Yes problem with selecting the right element. But like i said, namespaces should solve that problem. To be clear, i dont have any control over what gets stored inside element. So i can only think about some valid xml, but that would cause problem with xpath queries. – Jimm May 08 '12 at 22:01
2

You probably don't want to be nesting XML like that if you can help it. But, if you have to, then use CDATA.

Here's a reference to CDATA

Community
  • 1
  • 1
DennisG
  • 71
  • 2
2

Two possible solutions:

  1. escape the strings/xml data before you write your record elements.

  2. use CDATA sections

Colin D
  • 5,400
  • 1
  • 21
  • 35