搭配 FOR XML 使用 PATH 模式Use PATH Mode with FOR XML

適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database

使用 FOR XML 建構 XML所述,PATH 模式提供比較簡單的方式來混合元素與屬性。As described in Constructing XML Using FOR XML, the PATH mode provides a simpler way to mix elements and attributes. PATH 模式也是導入其他巢狀以代表複雜屬性的較簡單方式。PATH mode is also a simpler way to introduce additional nesting for representing complex properties. 您可以使用 FOR XML EXPLICIT 模式查詢來建構從資料列集而來的這類 XML,但是 PATH 模式對於可能會比較繁雜的 EXPLICIT 模式查詢提供較簡單的替代方案。You can use FOR XML EXPLICIT mode queries to construct such XML from a rowset, but the PATH mode provides a simpler alternative to the potentially cumbersome EXPLICIT mode queries. PATH 模式還可撰寫巢狀 FOR XML 查詢及 TYPE 指示詞,以傳回 xml 類型執行個體,這將可讓您撰寫較不複雜的查詢。PATH mode, together with the ability to write nested FOR XML queries and the TYPE directive to return xml type instances, allows you to write queries with less complexity.

在 PATH 模式中,資料行名稱或資料行別名是被視為 XPath 運算式。In PATH mode, column names or column aliases are treated as XPath expressions. 這些運算式指出值如何對應至 XML。These expressions indicate how the values are being mapped to XML. 每個 XPath 運算式都是提供項目類型的相對 XPath,這些項目類型包括屬性、元素、純量值、將會產生與資料列元素相對的節點名稱與階層。Each XPath expression is a relative XPath that provides the item type., such as the attribute, element, and scalar value, and the name and hierarchy of the node that will be generated relative to the row element.

本章節描述各種條件下資料列集中的對應資料行,並提供範例。This section describes mapping columns in a rowset under various conditions, and provides examples.

本節內容In This Section

另請參閱See Also

使用 WITH XMLNAMESPACES 將命名空間加入至查詢 Add Namespaces to Queries with WITH XMLNAMESPACES
SELECT (Transact-SQL) SELECT (Transact-SQL)
FOR XML (SQL Server)FOR XML (SQL Server)