如果在 XSD 中定义,则验证,否则 - 允许,但不验证
If defined in XSD, then validate, otherwise - allow, but do not validate
一份XML文档如下:
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost/app/myschema.xsd myschema.xsd">
<metadata>
<source appVer="2.10.0.3" structure="2.10.18.34" sequence="00000001" dt="2014-08-26T11:13:15"/>
</metadata>
<firstItemStorage>
<row col1="..." col2="..." />
<row col1="..." col2="..." />
<row col1="..." col2="..." />
</firstItemStorage>
<secondItemStorage>
<row col1="..." col2="..." />
<row col1="..." col2="..." />
<row col1="..." col2="..." />
</secondItemStorage>
<anyOtherElement>
<!-- Any XML -->
</anyOtherElement>
</data>
我想定义如下:
- 文档的根元素必须是
data
。
- 它里面可以有任何顺序的任何元素。
metadata
元素是必需的并且具有严格的结构。
- 不需要任何其他元素并且结构松散。
- 当
firstItemStorage
和 secondItemStorage
等元素在 XSD 模式中定义并出现在 XML 文档中时,应验证它们为零或无限制 row
具有零个或无限个属性的元素。定义的属性被验证,未定义的属性被禁止。
- 当 XSD 架构中未定义的任何其他元素出现在 XML 文档中时,不会发生验证错误并且不会验证它们的内容。
我的架构如下:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="metadata" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="source">
<xs:complexType>
<xs:attribute name="appVer" type="xs:string" use="required"/>
<xs:attribute name="structure" type="xs:string" use="required"/>
<xs:attribute name="sequence" type="xs:unsignedInt" use="required"/>
<xs:attribute name="dt" type="xs:dateTime" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="firstItemStorage" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="row" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<!-- Attributes could have any order -->
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="sid" type="xs:int" />
<xs:attribute name="id_group" type="xs:int" />
<xs:attribute name="consum" type="xs:double" />
<xs:attribute name="overloadlimit" type="xs:double" />
<xs:attribute name="upd" type="xs:dateTime" />
<!-- Any other attributes are prohibited -->
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="secondItemStorage" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="row" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<!-- Attributes could have any order -->
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="meaning" type="xs:string" />
<xs:attribute name="visible" type="xs:boolean" />
<xs:attribute name="a_counter" type="xs:boolean" />
<xs:attribute name="activities" type="xs:boolean" />
<xs:attribute name="upd" type="xs:dateTime" />
<!-- Any other attributes are prohibited -->
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Any other elements are allowed, but not validated -->
<xs:any />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
我对 xs:any
元素有问题:
any
元素不仅允许任何未知元素,还允许已知元素的任何属性
- 我收到来自 Visual Studio 的警告:
Warning 26 Wildcard '##any' allows element 'operations', and causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence.
没有 any
元素 当 XSD 中未定义的元素出现在 XML 文档中时,我会生成错误。
如何安全地定义我想要的?
在 XSD 1.1 中,您不会在特定元素粒子和通配符粒子之间产生任何歧义;特定粒子具有更高的优先级。如果您迁移到 XSD 1.1(这意味着使用非 Microsoft 工具),您会发现更容易满足您的要求。
如果您准备将元数据元素限制在第一位,那么您可以使用 (metadata, xs:any*) 的内容模型来执行此操作(即使在 XSD 1.0 中),其中 xs:any
有 processContents="lax"。松散验证基本上意味着如果有全局元素声明,则对其进行验证,否则不进行验证。然后,您的 firstItemStorage
和 secondItemStorage
元素声明必须成为全局元素声明(xs:element
作为 xs:schema
的子元素)。
迁移到 XSD 1.1 后,您可以解除元数据必须先出现的限制(我不确定您是否要将其限制为仅出现一次)。
一份XML文档如下:
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost/app/myschema.xsd myschema.xsd">
<metadata>
<source appVer="2.10.0.3" structure="2.10.18.34" sequence="00000001" dt="2014-08-26T11:13:15"/>
</metadata>
<firstItemStorage>
<row col1="..." col2="..." />
<row col1="..." col2="..." />
<row col1="..." col2="..." />
</firstItemStorage>
<secondItemStorage>
<row col1="..." col2="..." />
<row col1="..." col2="..." />
<row col1="..." col2="..." />
</secondItemStorage>
<anyOtherElement>
<!-- Any XML -->
</anyOtherElement>
</data>
我想定义如下:
- 文档的根元素必须是
data
。 - 它里面可以有任何顺序的任何元素。
metadata
元素是必需的并且具有严格的结构。- 不需要任何其他元素并且结构松散。
- 当
firstItemStorage
和secondItemStorage
等元素在 XSD 模式中定义并出现在 XML 文档中时,应验证它们为零或无限制row
具有零个或无限个属性的元素。定义的属性被验证,未定义的属性被禁止。 - 当 XSD 架构中未定义的任何其他元素出现在 XML 文档中时,不会发生验证错误并且不会验证它们的内容。
我的架构如下:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="metadata" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="source">
<xs:complexType>
<xs:attribute name="appVer" type="xs:string" use="required"/>
<xs:attribute name="structure" type="xs:string" use="required"/>
<xs:attribute name="sequence" type="xs:unsignedInt" use="required"/>
<xs:attribute name="dt" type="xs:dateTime" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="firstItemStorage" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="row" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<!-- Attributes could have any order -->
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="sid" type="xs:int" />
<xs:attribute name="id_group" type="xs:int" />
<xs:attribute name="consum" type="xs:double" />
<xs:attribute name="overloadlimit" type="xs:double" />
<xs:attribute name="upd" type="xs:dateTime" />
<!-- Any other attributes are prohibited -->
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="secondItemStorage" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="row" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<!-- Attributes could have any order -->
<xs:attribute name="id" type="xs:int" />
<xs:attribute name="meaning" type="xs:string" />
<xs:attribute name="visible" type="xs:boolean" />
<xs:attribute name="a_counter" type="xs:boolean" />
<xs:attribute name="activities" type="xs:boolean" />
<xs:attribute name="upd" type="xs:dateTime" />
<!-- Any other attributes are prohibited -->
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Any other elements are allowed, but not validated -->
<xs:any />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
我对 xs:any
元素有问题:
any
元素不仅允许任何未知元素,还允许已知元素的任何属性- 我收到来自 Visual Studio 的警告:
Warning 26 Wildcard '##any' allows element 'operations', and causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence.
没有 any
元素 当 XSD 中未定义的元素出现在 XML 文档中时,我会生成错误。
如何安全地定义我想要的?
在 XSD 1.1 中,您不会在特定元素粒子和通配符粒子之间产生任何歧义;特定粒子具有更高的优先级。如果您迁移到 XSD 1.1(这意味着使用非 Microsoft 工具),您会发现更容易满足您的要求。
如果您准备将元数据元素限制在第一位,那么您可以使用 (metadata, xs:any*) 的内容模型来执行此操作(即使在 XSD 1.0 中),其中 xs:any
有 processContents="lax"。松散验证基本上意味着如果有全局元素声明,则对其进行验证,否则不进行验证。然后,您的 firstItemStorage
和 secondItemStorage
元素声明必须成为全局元素声明(xs:element
作为 xs:schema
的子元素)。
迁移到 XSD 1.1 后,您可以解除元数据必须先出现的限制(我不确定您是否要将其限制为仅出现一次)。