简体   繁体   中英

Defining embedded XML in XMLSchema

I have to validate XML file that contains embedded XML, with XML Schema. Correctness of the inner XML doesn't interest me. Which type should I use in XML Schema for this type of content? Simple xs:string doesn't work.

You have a number of alternatives for embedding XML within another XML document.

xs:any:

You can use the xs:any type in your schema ( link ). However you will need to include a schema for the embedded XML (which will be used to validate it).

Alternatively, xs:string can be made to work if you either embed the inner XML within a CDATA section or escape all the < and & characters.

CDATA section:

<InnerXml>
  <![CDATA[
    <InnerXmlRoot>
      <InnerXmlContent>One & Two</InnerXmlContent>
    </InnerXmlRoot>
  ]]>
</InnerXml> 

The only thing you need to be careful of with this approach is that the inner XML cannot contain CDATA sections as the allowed content cannot contain ]]> (see the XML specification ).

Character escaping:

<InnerXml>
  &lt;InnerXmlRoot>
    &lt;InnerXmlContent>One &amp; Two&lt;/InnerXmlContent>
  &lt;/InnerXmlRoot>
</InnerXml> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM