Как вернуть столбцы как имена элементов вместо элементов полей с атрибутами name - PullRequest
1 голос
/ 20 января 2012

Я экспортирую данные из SQL Server в XML с конечной целью преобразования их в XSLT. Я хотел бы иметь возможность использовать экспортированные данные и ссылаться на них по имени элемента. Вывод моих данных XML выглядит следующим образом:

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

Я бы хотел, чтобы вместо этого было имя элемента:

<InvoiceNo>641718</InvoiceNo>

У меня есть 3 функции. Функция, которая создает заголовок, один для деталей и один, который экспортирует весь счет в XML.

Вот функция, которая создает XML. Я пробовал разные варианты, чтобы заставить это работать, но я не мог придумать правильный синтаксис. Это, конечно, предполагает, что внесение этого изменения сейчас облегчит выполнение XSLT в будущем, если потребуются дополнительные преобразования. В противном случае я мог бы использовать что-то вроде field[@name = "CustomerPO"] в моем XSL.

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 Ответ

3 голосов
/ 20 января 2012

Если вы просто запросите вашу таблицу как можно проще, вы получите то, что вы хотите.

Попробуйте что-то вроде этого.

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...