定義 XML 資料的序列化Define the Serialization of XML Data

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

將 XML 資料類型明確或隱含轉換成 SQL 字串或二進位類型時,會根據本主題中所列的規則來序列化 XML 資料類型的內容。When casting the xml data type explicitly or implicitly to a SQL string or binary type, the content of the xml data type will be serialized according to the rules outlined in this topic.

序列化編碼Serialization Encoding

如果 SQL 目標類型是 VARBINARY,其結果會以 UTF-16 序列化,前面有 UTF-16 位元組順序標示,但沒有 XML 宣告。If the SQL target type is VARBINARY, the result is serialized in UTF-16 with a UTF-16-byte order mark in front, but without an XML declaration. 如果目標類型太小,則會引發錯誤。If the target type is too small, an error is raised.

例如:For example:

select CAST(CAST(N'<Δ/>' as XML) as VARBINARY(MAX))  

以下是結果:This is the result:

0xFFFE3C0094032F003E00  

如果 SQL 目標類型是 NVARCHAR 或 NCHAR,則結果會以 UTF-16 序列化,但前面沒有位元組順序標示,也沒有 XML 宣告。If the SQL target type is NVARCHAR or NCHAR, the result is serialized in UTF-16 without the byte order mark in front and without an XML declaration. 如果目標類型太小,則會引發錯誤。If the target type is too small, an error is raised.

例如:For example:

select CAST(CAST(N'<Δ/>' as XML) as NVARCHAR(MAX))  

以下是結果:This is the result:

<Δ/>  

如果 SQL 目標類型是 VARCHAR 或 NCHAR,其結果會以對應於資料庫定序字碼頁的編碼序列化,但沒有位元組順序標示或 XML 宣告。If the SQL target type is VARCHAR or NCHAR, the result is serialized in the encoding that corresponds to the database's collation code page without a byte order mark or XML declaration. 如果目標類型太小,或值無法對應至目標定序字碼頁,則會引發錯誤。If the target type is too small or the value cannot be mapped to the target collation code page, an error is raised.

例如:For example:

select CAST(CAST(N'<Δ/>' as XML) as VARCHAR(MAX))  

如果目前定序的字碼頁無法表示 Unicode 字元 Δ,這樣會產生錯誤,否則會以特定編碼表示該字元。This may result in an error, if the current collation's code page cannot represent the Unicode character Δ, or it will represent it in the specific encoding.

將 XML 結果傳回至用戶端時,資料是以 UTF-16 編碼格式傳送。When returning XML results to the client side, the data will be sent in UTF-16 encoding. 接著,用戶端提供者會根據其 API 規則來公開此資料。The client-side provider will then expose the data according to its API rules.

XML 結構的序列化Serialization of the XML Structures

xml 資料類型的內容以一般方式序列化。The content of an xml data type is serialized in the usual way. 特別是,元素節點是對應到元素標示,而文字節點則對應到文字內容。Specifically, element nodes are mapped to element markup, and text nodes are mapped to text content. 不過,下列章節將描述字元在何種情況下實體化,以及具類型的不可部份完成值如何序列化。However, the circumstances under which characters are entitized and how typed atomic values are serialized are described in the following sections.

XML 字元在序列化期間的實體化Entitization of XML Characters During Serialization

每一個已序列化的 XML 結構應該能夠加以重新剖析。Every serialized XML structure should be capable of being reparsed. 因此,有些字元必須以實體化方式來序列化,以透過 XML 剖析器的正規化階段保留字元的反覆存取功能。Therefore, some characters have to be serialized in an entitized way to preserve the round-trip capability of the characters through the XML parser's normalization phase . 不過,有些字元必須實體化,使文件能夠有完善的格式,以便加以剖析。However, some characters have to be entitized so that the document is well-formed and, therefore, able to be parsed. 以下是在序列化期間所套用的實體化規則:Following are the entitization rules that apply during serialization:

  • 若字元 &、< 和 > 是出現在屬性值或元素內容中,則一律分別實體化成 &、< 和 >。The characters &, <, and > are always entitized to &, <, and > respectively, if they occur inside an attribute value or element content.

  • 因為 SQL Server 使用引號 (U+0022) 來括住屬性值,所以屬性值中的引號會實體化成 "。Because SQL Server uses a quotation mark (U+0022) for enclosing attribute values, the quotation mark in attribute values is entitized as ".

  • 只在伺服器上進行轉換時,Surrogate 字組會實體化成單一數字字元參考。A surrogate pair is entitized as a single numeric character reference, when casting on the server only. 例如,Surrogate 字組 U+D800 U+DF00 會實體化成數字字元參考 &#x00010300;。For example the surrogate pair U+D800 U+DF00 is entitized to the numeric character reference &#x00010300;.

  • 為避免在剖析期間將定位字元 (U+0009) 和換行符號 (LF、U+000A) 正規化,已在屬性值內分別實體化成其數字字元參考 &#x9; 和 &#xA;。To protect a TAB (U+0009) and a linefeed (LF, U+000A) from being normalized during parsing, they are entitized to their numeric character references &#x9; and &#xA; respectively, inside attribute values.

  • 為避免在剖析期間將歸位字元 (CR、U+000D) 正規化,已在屬性值和元素內容內實體化成其數字字元參考 &#xD;。To prevent a carriage return (CR, U+000D) from being normalized during parsing, it is entitized to its numeric character reference, &#xD; inside both attribute values and element content.

  • 為了保護只包含空格的文字節點,其中一個空格字元 (通常是最後一個) 會實體化為其數字字元參考。To protect text nodes that only contain white space, one of the white-space characters, generally the last one, is entitized as its numeric character reference. 如此一來,不論剖析期間的空格處理設定為何,重新剖析作業都會保留空格文字節點。In this way, reparsing preserves the white-space text node, regardless of the setting of the white-space handling during parsing.

例如:For example:

declare @u NVARCHAR(50)  
set @u = N'<a a="  
    '+NCHAR(0xD800)+NCHAR(0xDF00)+N'>">   '+NCHAR(0xA)+N'</a>'  
select CAST(CONVERT(XML,@u,1) as NVARCHAR(50))  

以下是結果:This is the result:

<a a="  
    𐌀>">     
</a>  

如果您不想套用最後一個空格的保護規則,您可以在從 XML 轉換成字串或二進位類型時,使用明確的 CONVERT 選項 1。If you do not want to apply the last white-space protection rule, you can use the explicit CONVERT option 1 when casting from xml to a string or binary type. 例如,若要避免實體化,您可以執行下列動作:For example, to avoid entitization, you can do the following:

select CONVERT(NVARCHAR(50), CONVERT(XML, '<a>   </a>', 1), 1)  

請注意, query() 方法 (XML 資料類型) 會產生 XML 資料類型執行個體。Note that, the query() Method (xml Data Type) results in an xml data type instance. 因此,轉換成字串或二進位類型的 query() 方法的任何結果,將根據前述規則而實體化。Therefore, any result of the query() method that is cast to a string or binary type is entitized according to the previously described rules. 如果您要取得未實體化的字串值,請改用 value() 方法 (XML 資料類型)If you want to obtain the string values that are not entitized, you should use the value() Method (xml Data Type) instead. 以下是使用 query() 方法的範例:Following is an example of using the query() method:

declare @x xml  
set @x = N'<a>This example contains an entitized char: <.</a>'  
select @x.query('/a/text()')  

以下是結果:This is the result:

This example contains an entitized char: <.  

以下是使用 value() 方法的範例:Following is an example of using the value() method:

select @x.value('(/a/text())[1]', 'nvarchar(100)')  

以下是結果:This is the result:

This example contains an entitized char: <.  

序列化 XML 類型的資料類型Serializing a Typed xml Data Type

具類型的 xml 資料類型執行個體根據其 XML 結構描述類型來包含具類型的值。A typed xml data type instance contains values that are typed according to their XML schema types. 這些值根據其 XML 結構描述類型而序列化,並使用與 XQuery 轉換成 xs:string 時產生的相同格式。These values are serialized according to their XML schema type in the same format as the XQuery cast to xs:string produces. 如需詳細資訊,請參閱 XQuery 中的類型轉換規則For more information, see Type Casting Rules in XQuery.

例如,xs:double 值 1.34e1 序列化為 13.4,如下列範例所顯示:For example, the xs:double value 1.34e1 is serialized to 13.4 as shown in the following example:

declare @x xml  
set @x =''  
select CAST(@x.query('1.34e1') as nvarchar(50))  

這會傳回字串值 13.4。This returns the string value 13.4.

另請參閱See Also

XQuery 中的類型轉換規則 Type Casting Rules in XQuery
CAST 和 CONVERT (Transact-SQL)CAST and CONVERT (Transact-SQL)