問題描述
我已經搜索了一段時間,得出的結論是可能無法更改每個屬性值的驗證.
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"節(jié)點,都有一個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>
我想創(chuàng)建一個 XSD* 來檢查這兩種類型,這可能嗎?如果是這樣,如何?
I want to create one XSD* who whould check both types, is this possible? And if so, how?
- 它必須是一個 XSD,因為我想將 XSD 放在 MSSQL 數(shù)據(jù)庫表的 XML 列上.
推薦答案
您說得對,XSD 1.0 是不可能的,它是 MSSQL 支持的唯一 XSD 版本.你能得到的最好的方法是在 a
和 bla
之間創(chuàng)建一個選擇,也許對屬性 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.
這篇關于如何創(chuàng)建在屬性值驗證方面不同的 XSD?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!