如何将列作为元素名称而不是具有name属性的字段元素返回

时间:2012-01-20 13:28:02

标签: xml sql-server-2008 xslt

我正在以XML格式从SQL Server导出数据,最终目标是使用XSLT进行转换。我想做的是能够按元素名称使用和引用导出的数据。我的XML数据输出如下:

<field name="InvoiceNo">641718</field>

我希望它具有元素名称:

<InvoiceNo>641718</InvoiceNo>

我有3个功能。构建标题的函数,一个用于详细信息,另一个用于将整个发票导出为XML。

这是构建XML的函数。我尝试了不同的变化来使这个工作,但我无法提出正确的语法。这当然假设如果需要进行额外的转换,现在进行此更改将使将来更容易进行XSLT。否则我可以在我的XSL中使用类似field[@name = "CustomerPO"]的内容。

DECLARE @Invoice XML
       SET @Invoice =
       (SELECT 
        (SELECT [field/@name] = 'InvoiceNo' , [field] = InvoiceNo FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SONo', [field] = SONo FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'CustomerPO', [field] =CustomerPO FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'InvoiceDate', [field] = REPLACE(CONVERT(varchar,InvoiceDate,102),'.','-') FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'DueDate', [field] = DueDate FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipVia', [field] = ShipVia FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipViaAcctNo', [field] = ShipViaAcctNo FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'FOB', [field] = FOB FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShippedDate', [field] = REPLACE(CONVERT(varchar,ShippedDate,102),'.','-') FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'OurRef', [field] = OurRef FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'YourRef', [field] = YourRef FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Project', [field] = Project FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'CustType', [field] = CustType FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Terms', [field] = Terms FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'DomesticAWB', [field] = DomesticAWB FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'IntlAWB', [field] = IntlAWB FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Origin', [field] = Origin FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Transport', [field] = Transport FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Nationality', [field] = Nationality FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'TripFlight', [field] = TripFlight FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'OurContact', [field] = OurContact FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Phone', [field] = Phone FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Fax', [field] = Fax FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Email', [field] = Email FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillTo', [field] = BillTo FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToAttn', [field] = BillToAttn FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToAdd1', [field] = BillToAdd1  FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToAdd2', [field] = BillToAdd2 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToAdd3', [field] = BillToAdd3 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToCity', [field] = BillToCity FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToState', [field] = BillToState FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToZip', [field] = BillToZip FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToCountry', [field] = BillToCountry FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToTelephone', [field] = BillToPhone FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'BillToFax', [field] = BillToFax FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldTo', [field] = SoldTo FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToAttn', [field] = SoldToAttn FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToAdd1', [field] = SoldToAdd1 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToAdd2', [field] = SoldToAdd2 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToAdd3', [field] = SoldToAdd3 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToCity', [field] = SoldToCity FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToState', [field] = SoldToState FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToZip', [field] = SoldToZip FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToCountry', [field] = SoldToCountry FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToTelephone', [field] = SoldToPhone FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'SoldToFax', [field] = SoldToFax FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipTo', [field] = ShipTo FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipTAttn', [field] = ShipToAttn FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToAdd1', [field] = ShipToAdd1 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToAdd2', [field] = ShipToAdd2 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToAdr3', [field] = ShipToAdd3 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToCity', [field] = ShipToCity FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToState', [field] = ShipToState FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToZip', [field] = ShipToZip FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToCountry', [field] = ShipToCountry FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToTelephone', [field] = ShipToTelephone FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'ShipToFax', [field] = ShipToFax FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Subtotal', [field] = Subtotal FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Discount', [field] = Discount FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Taxes', [field] = Taxes FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Misc1', [field] = Misc1 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Misc2', [field] = Misc2 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Misc3', [field] = Misc3 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Misc4', [field] = Misc4 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Misc5', [field] = Misc5 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'Misc6', [field] = Misc6 FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'TaxA', [field] = TaxA FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'TaxB', [field] = TaxB FOR XML PATH(''), TYPE)
        ,(SELECT [field/@name] = 'FixedMessage', [field] = FixedMessage FOR XML PATH(''), TYPE)
        ,(
           SELECT 
             (SELECT [field/@name] = 'Line', [field] = Line FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'PartNumber', [field] =  PartNumber FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'Description', [field] =  Descr FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'PickTicketPackingSlipNo', [field] =  PickTicketPackingSlipNo FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'Notes', [field] = Notes FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'NSN', [field] =  NSN FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'SerialNo', [field] =  SerialNo FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'Cnd', [field] =  Cnd FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'FAA', [field] =  FAA  FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'QtyOrdered', [field] =  QtyOrdered FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'UOM', [field] =  UOM FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'Shipped', [field] =  Shipped FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'BackOrder', [field] =  BackOrder FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'UnitPrice', [field] =  UnitPrice FOR XML PATH(''), TYPE)
            ,(SELECT [field/@name] = 'Amount', [field] =  Amount FOR XML PATH(''), TYPE)
        FROM dbo.fnInvoiceDetail(@DOC_NO) FOR XML PATH('detail'), TYPE--, ELEMENTS
        ) as 'Detail' 
    FROM dbo.fnInvoiceHeader(@invoiceNo)
    FOR XML PATH('header'),TYPE 
    ,ROOT('invoice'))

RETURN @Invoice

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

如果您只是尽可能简单地查询您的表格,那么您将得到您想要的内容。

尝试这样的事情。

declare @T table
(
  InvoiceNo int,
  SONo int
)

insert into @T values
(641718, 1),
(641719, 2)

select InvoiceNo,
       SONo
from @T
for xml path('header'), root('invoice')

结果:

<invoice>
  <header>
    <InvoiceNo>641718</InvoiceNo>
    <SONo>1</SONo>
  </header>
  <header>
    <InvoiceNo>641719</InvoiceNo>
    <SONo>2</SONo>
  </header>
</invoice>