萬用字元元件和內容驗證Wildcard Components and Content Validation

適用於: 是SQL Server 否Azure SQL Database 否Azure SQL 資料倉儲 否平行處理資料倉儲 APPLIES TO: yesSQL Server noAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

萬用字元元件可用以增加可出現在內容模型中的內容彈性。Wildcard components are used to increase flexibility in what is allowed to appear in a content model. 在 XSD 語言中,可透過下列方式支援這些元件:These components are supported in the XSD language in the following ways:

  • 元素萬用字元元件。Element wildcard components. 這些是以 <xsd:any> 項目表示。These are represented by the <xsd:any> element.

  • 屬性萬用字元元件。Attribute wildcard components. 這些是以 <xsd:anyAttribute> 項目表示。These are represented by the <xsd:anyAttribute> element.

<xsd:any><xsd:anyAttribute> 這兩個萬用字元項目都支援使用 processContents 屬性。Both wildcard character elements, <xsd:any> and <xsd:anyAttribute>, support the use of a processContents attribute. 這可讓您指定一個值,以指出 XML 應用程式如何處理與這些萬用字元元素關聯的文件內容驗證。This lets you specify a value that indicates how XML applications handle the validation of document content associated with these wildcard character elements. 以下是不同的值所產生的效果:These are the different values and their effect:

  • strict 值指定完整驗證內容。The strict value specifies that the contents are fully validated.

  • skip 值指定不驗證內容。The skip value specifies that the contents are not validated.

  • lax 值指定只會驗證可以使用結構描述定義的元素與屬性。The lax value specifies that only elements and attributes for which schema definitions are available are validated.

Lax 驗證和 xs:anyType 元素Lax Validation and xs:anyType Elements

XML 結構描述規格會針對 anyType 類型的元素使用 lax 驗證。The XML Schema specification uses lax validation for elements of the anyType type. 因為 SQL Server 2005 (9.x)SQL Server 2005 (9.x) 不支援 Lax 驗證,因此會對 anyType的元素套用 Strict 驗證。Because SQL Server 2005 (9.x)SQL Server 2005 (9.x) did not support lax validation, strict validation was applied for elements of the anyType. SQL Server 2008SQL Server 2008開始,Lax 驗證就有受到支援。Beginning with SQL Server 2008SQL Server 2008, lax validation is supported. anyType 類型的元素內容將會使用 Lax 驗證加以驗證。Content of elements of type anyType will be validated using lax validation.

下列範例說明 Lax 驗證。The following example illustrates the lax validation. e 結構描述元素的類型為 anyTypeThe schema element e is of the anyType type. 此範例會建立具類型的 xml 變數,並說明 anyType 類型元素的 Lax 驗證。The example creates typed xml variables and illustrates the lax validation of the element of the anyType type.

CREATE XML SCHEMA COLLECTION SC AS '  
<schema xmlns="http://www.w3.org/2001/XMLSchema"   
        targetNamespace="http://ns">  
   <element name="e" type="anyType"/>  
   <element name="a" type="byte"/>  
   <element name="b" type="string"/>  
 </schema>'  
GO  

下列範例會成功,因為 <e> 的驗證是成功的:The following example succeeds, because the validation of <e> is successful:

DECLARE @var XML(SC)  
SET @var = '<e xmlns="http://ns"><a>1</a><b>data</b></e>'  
GO  

下列範例將會成功。The following example succeeds. 即使此結構描述中未定義任何 <c> 元素,還是可接受此結構描述:The instance is accepted, even though no element <c> is defined in the schema:

DECLARE @var XML(SC)  
SET @var = '<e xmlns="http://ns"><a>1</a><c>Wrong</c><b>data</b></e>'  
GO  

下列範例中的 XML 執行個體會遭到拒絕,因為 <a> 元素的定義不允許字串值。The XML instance in the following example is rejected, because the definition of the <a> element does not allow a string value.

DECLARE @var XML(SC)  
SET @var = '<e xmlns="http://ns"><a>Wrong</a><b>data</b></e>'  
SELECT @var  
GO  

另請參閱See Also

伺服器上 XML 結構描述集合的需求與限制Requirements and Limitations for XML Schema Collections on the Server