如何创建在属性值验证方面不同的 XSD?
我已经搜索了一段时间,得出的结论是可能无法更改每个属性值的验证.
I have been searching for a while now, and came to the conclusion it may not be possible to change the validation per value of an attribute.
例如我有两个action"节点,都有一个type"属性和两个元素(name"和description")
For example I have two "action" nodes, both with a "type" attribute and two elements ("name" and "description")
仅当type"属性的值为1"时,它有一个带有abc"子元素的a"元素,当type"属性为2"时,它有一个带有的"元素bla"然而"子元素.
Only when the value of the "type" attribute is "1" it has an "a" element with "abc" child elements and when the "type" attibute is "2" it has a "bla" element with "yet" child elements.
类型 1 的示例
<action type="1">
<name>yup</name>
<description>yyy</description>
<a>
<abc>false</abc>
</a>
</action>
类型 2 的示例
<action type="2">
<name>yup2</name>
<description>RRR</description>
<bla>
<yet />
</bla>
</action>
我想创建一个 XSD* 来检查这两种类型,这可能吗?如果是这样,如何?
I want to create one XSD* who whould check both types, is this possible? And if so, how?
- 它必须是一个 XSD,因为我想将 XSD 放在 MSSQL 数据库表的 XML 列上.
推荐答案
您说得对,XSD 1.0 是不可能的,它是 MSSQL 支持的唯一 XSD 版本.你能得到的最好的方法是在 a
和 bla
之间创建一个选择,也许对属性 type
值等进行一些限制.下面是一个插图.
You are right, it is not possible with XSD 1.0 which is the only XSD version supported by MSSQL. The best you can get is to create a choice between a
and bla
, maybe place some constraints on attribute type
values, etc. Below is an illustration.
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="action">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="description" type="xs:string" />
<xs:choice>
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element name="abc" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="bla">
<xs:complexType>
<xs:sequence>
<xs:element name="yet" type="xs:anyType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="type" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
如果您控制 XML 结构,并且仍然想使用某些属性来控制内容模型,那么 xsi:type 是 XSD 1.0 中唯一的方法.
If you control the XML structure, and still want to use some attribute to control the content model, then xsi:type is the only way to do it in XSD 1.0.
相关文章