在 SQL Server 2005 Service Pack 1 (SP1) 之前,SQL Server 會拒絕具有不具決定性內容模型的 XML 架構。
不過,從 SQL Server 2005 SP1 開始,如果發生條件約束為 0,1 或未繫結,則會接受不具決定性的內容模型。
範例:拒絕不具決定性的內容模型
下列範例會嘗試使用不具決定性的內容模型來建立 XML 架構。 程式碼失敗,因為目前還不清楚<root>元素是否應該有包含兩個<a>元素的序列,或者<root>元素是否應該有兩個序列,每個序列都包含一個<a>元素。
CREATE XML SCHEMA COLLECTION MyCollection AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="root">
<complexType>
<sequence minOccurs="1" maxOccurs="2">
<element name="a" type="string" minOccurs="1" maxOccurs="2"/>
</sequence>
</complexType>
</element>
</schema>
'
GO
您可以將發生條件約束移至唯一位置,以修正架構。 例如,限制可以移至所含的序列粒子:
<sequence minOccurs="1" maxOccurs="4">
<element name="a" type="string" minOccurs="1" maxOccurs="1"/>
</sequence>
或者,條件約束可以移至包含的元素中:
<sequence minOccurs="1" maxOccurs="1">
<element name="a" type="string" minOccurs="1" maxOccurs="4"/>
</sequence>
範例:接受不具決定性的內容模型
在 SQL Server 2005 SP1 之前,SQL Server 版本會拒絕下列架構。
CREATE XML SCHEMA COLLECTION MyCollection AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="root">
<complexType>
<sequence minOccurs="0" maxOccurs="unbounded">
<element name="a" type="string" minOccurs="0" maxOccurs="1"/>
<element name="b" type="string" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
</schema>
'
GO