简体   繁体   English

在XMLSchema中定义嵌入式XML

[英]Defining embedded XML in XMLSchema

I have to validate XML file that contains embedded XML, with XML Schema. 我必须使用XML Schema验证包含嵌入式XML的XML文件。 Correctness of the inner XML doesn't interest me. 内部XML的正确性使我不感兴趣。 Which type should I use in XML Schema for this type of content? 我应该在XML模式中使用哪种类型的内容? Simple xs:string doesn't work. 简单的xs:string不起作用。

You have a number of alternatives for embedding XML within another XML document. 您可以使用多种方法将XML嵌入另一个XML文档中。

xs:any:

You can use the xs:any type in your schema ( link ). 您可以在架构( link )中使用xs:any类型。 However you will need to include a schema for the embedded XML (which will be used to validate it). 但是,您将需要包括用于嵌入式XML的架构(将用于验证它)。

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. 另外,如果您将内部XML嵌入CDATA节中或转义所有<&字符,则可以使xs:string工作。

CDATA section: CDATA部分:

<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 ). 使用这种方法时,需要注意的唯一事情是内部XML不能包含CDATA节,因为允许的内容不能包含]]> (请参阅XML规范 )。

Character escaping: 字符转义:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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