承接上篇,PATH 模式提供比 EXPLICIT 更簡潔的方式建構靈活 XML 結構。
WITH XMLNAMESPACES (DEFAULT 'https://blog.csdn.net/zhzuo')
SELECT ProductID AS 'Product/@ProductID',
[Name] AS 'Product/Name'
FROM Production.Product
WHERE ProductSubcategoryID = 9
FOR XML PATH(''), ROOT('Products');
輸出:
<Products xmlns="https://blog.csdn.net/zhzuo">
<Product ProductID="...">
<Name>Rear Derailleur</Name>
</Product>
<Product ProductID="...">
<Name>Front Derailleur</Name>
</Product>
</Products>
PATH(''):不產生每列包裹元素,直接由別名路徑決定結構@ 前綴 = 屬性,無前綴 = 子元素ROOT('Products'):最外層根元素| 平台 | 效益 |
|---|---|
| .NET (ADO.NET) | 直接取得 XmlReader/XElement,省去 XSLT 轉換,直接服務上層模組 |
| BizTalk Server | SQL Adapter 呼叫更彈性,降低 Schema 設計複雜度 |
注意:BizTalk 2006 R2 的 SQL Adapter Wizard 尚不支援自動從增強 FOR XML 預存程序產生 Schema,需手寫 XSD 或用 Visual Studio 設計器對應實際輸出格式。
下篇總結關鍵語法對照與最佳實務檢核表。