我想解析下面的@xml并生成一个像这样的表:
declare @xml XML = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInvoiceResponse xmlns="http://www.myCompany.com.au/gateway2/invoicemanagement">
<GetInvoiceResult>
<AccountNumber>54321</AccountNumber>
<InvoiceNumber>Inv10001</InvoiceNumber>
<Lines>
<InvoiceLine>
<Cost>5.86</Cost>
<Ean>Ean111</Ean>
<QuantitySupplied>1</QuantitySupplied>
</InvoiceLine>
<InvoiceLine>
<Cost>4.00</Cost>
<Ean>Ean222</Ean>
<QuantitySupplied>2</QuantitySupplied>
</InvoiceLine>
</Lines>
<TotalCost>9.86</TotalCost>
</GetInvoiceResult>
</GetInvoiceResponse>
</soap:Body>
</soap:Envelope>';
我想解析@xml并生成带有标题和行的表。
+---------------+--------+------+-----------+
| InvoiceNumber | Ean | Cost | TotalCost |
+---------------+--------+------+-----------+
| Inv10001 | Ean111 | 5.86 | 9.86 |
| Inv10001 | Ean222 | 4.00 | 9.86 |
+---------------+--------+------+-----------+
答案 0 :(得分:1)
赞:
[45, 67, 34]
输出
declare @xml XML = '<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInvoiceResponse xmlns="http://www.myCompany.com.au/gateway2/invoicemanagement">
<GetInvoiceResult>
<AccountNumber>54321</AccountNumber>
<InvoiceNumber>Inv10001</InvoiceNumber>
<Lines>
<InvoiceLine>
<Cost>5.86</Cost>
<Ean>Ean111</Ean>
<QuantitySupplied>1</QuantitySupplied>
</InvoiceLine>
<InvoiceLine>
<Cost>4.00</Cost>
<Ean>Ean222</Ean>
<QuantitySupplied>2</QuantitySupplied>
</InvoiceLine>
</Lines>
<TotalCost>9.86</TotalCost>
</GetInvoiceResult>
</GetInvoiceResponse>
</soap:Body>
</soap:Envelope>';
WITH XMLNAMESPACES (DEFAULT 'http://www.myCompany.com.au/gateway2/invoicemanagement')
select n.value('InvoiceNumber[1]','varchar(15)') InvoiceNumber,
l.value('Ean[1]','varchar(20)') Ean,
l.value('Cost[1]','varchar(20)') Cost,
n.value('TotalCost[1]','decimal(10,2)') TotalCost
from @xml.nodes('//GetInvoiceResult') r(n)
cross apply r.n.nodes('Lines/InvoiceLine') lines(l)
严格来说,使用InvoiceNumber Ean Cost TotalCost
--------------- -------------------- -------------------- ---------------------------------------
Inv10001 Ean111 5.86 9.86
Inv10001 Ean222 4.00 9.86
运算符不是最佳形式,但这只是一个小文档,它使您不必声明soap名称空间并将目标节点引用为//