共用方式為


非決定性內容模型

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體

即使出現次數條件約束為 0、1 或未受約束,在 SQL Server 中仍可接受不具決定性的內容模型。

在 SQL Server 2005 (9.x) Service Pack 1 (SP1) 之前,SQL Server 會拒絕具有不具決定性之內容模型的 XML 結構描述。

範例:拒絕不具決定性的內容模型

下列範例會嘗試建立具有不具決定性之內容模型的 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 (9.x) 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

另請參閱