1

我正在尝试使用FOR XML子句EXPLICIT模式查询某些数据库,但我不知道如何在使用此模式时指定 XMLNAMESPACE 和模式,就像使用AUTO模式时的 WITH XMLNAMESPACE 或RAW

MSDN示例查询在其结果中将 指定Schema1为结果的架构XML

USE AdventureWorks2012;
GO
 SELECT  1 as Tag,
0 as Parent,
SalesOrderID  as [OrderHeader!1!SalesOrderID!id],
OrderDate     as [OrderHeader!1!OrderDate],
CustomerID    as [OrderHeader!1!CustomerID],
NULL          as [SalesPerson!2!SalesPersonID],
NULL          as [OrderDetail!3!SalesOrderID!idref],
NULL          as [OrderDetail!3!LineTotal],
NULL          as [OrderDetail!3!ProductID],
NULL          as [OrderDetail!3!OrderQty]
FROM   Sales.SalesOrderHeader
WHERE  SalesOrderID=43659 or SalesOrderID=43661
....
SELECT 3 as Tag,
 1 as Parent,
SOD.SalesOrderID,
NULL,
NULL,
SalesPersonID,
SOH.SalesOrderID,
LineTotal,
ProductID,
OrderQty   
FROM    Sales.SalesOrderHeader SOH,Sales.SalesOrderDetail SOD
 WHERE   SOH.SalesOrderID = SOD.SalesOrderID
 AND     (SOH.SalesOrderID=43659 or SOH.SalesOrderID=43661)
 ORDER BY [OrderHeader!1!SalesOrderID!id], [SalesPerson!2!SalesPersonID],
  [OrderDetail!3!SalesOrderID!idref],[OrderDetail!3!LineTotal]
 FOR XML EXPLICIT, XMLDATA

这是示例结果:

    <Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data"                   xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="OrderHeader" content="mixed" model="open">
    <AttributeType name="SalesOrderID" dt:type="id" />
    <AttributeType name="OrderDate" dt:type="dateTime" />
    <AttributeType name="CustomerID" dt:type="i4" />
    <attribute type="SalesOrderID" />
    <attribute type="OrderDate" />
    <attribute type="CustomerID" />
     </ElementType>
      <ElementType name="SalesPerson" content="mixed" model="open">
    <AttributeType name="SalesPersonID" dt:type="i4" />
    <attribute type="SalesPersonID" />
       </ElementType>
       <ElementType name="OrderDetail" content="mixed" model="open">
    <AttributeType name="SalesOrderID" dt:type="idref" />
    <AttributeType name="LineTotal" dt:type="number" />
    <AttributeType name="ProductID" dt:type="i4" />
    <AttributeType name="OrderQty" dt:type="i2" />
    <attribute type="SalesOrderID" />
    <attribute type="LineTotal" />
    <attribute type="ProductID" />
    <attribute type="OrderQty" />
        </ElementType>
      </Schema>
      <OrderHeader xmlns="x-schema:#Schema1" SalesOrderID="43659" OrderDate="2001-07-01T00:00:00"    CustomerID="676">
      <SalesPerson SalesPersonID="279" />
     <OrderDetail SalesOrderID="43659" LineTotal="10.373000" ProductID="712" OrderQty="2" />
     ...
    </OrderHeader>
     ...
4

1 回答 1

2

来自 MSDN 的查询包含一个架构,因为它使用XMLDATA指令。

指定应返回内联 XML 数据缩减 (XDR) 架构。该模式作为内联模式附加到文档中。有关工作示例,请参阅将 RAW 模式与 FOR XML 结合使用。

如果你想包含一个显式模式的命名空间,你必须自己添加它并相应地命名你的元素和属性。

declare @T table
(
  id int,
  title varchar(50)
)

insert into @T values(451, 'Receptionist')
insert into @T values(1025, 'Secretary')

select 1    as Tag,
       null as Parent,
       'xx.yy.zz' as [root!1!xmlns:ns],
       null as [ns:item!2!ns:id],
       null as [ns:item!2!ns:title]
union all
select 2    as Tag,
       1 as Parent,
       null,
       id,
       title
from @T 
for xml explicit

结果:

<root xmlns:ns="xx.yy.zz">
  <ns:item ns:id="451" ns:title="Receptionist" />
  <ns:item ns:id="1025" ns:title="Secretary" />
</root>
于 2012-07-09T14:36:41.907 回答