如何使用 Header 和 SQL 服务器中的行解析 SOAP XML 并显示为 table?
How to parse a SOAP XML with Header and Lines in SQL Server and show as table?
我想解析下面的 @xml 并生成一个 table,例如:
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 并生成带有 header 和行的 table。
+---------------+--------+------+-----------+
| InvoiceNumber | Ean | Cost | TotalCost |
+---------------+--------+------+-----------+
| Inv10001 | Ean111 | 5.86 | 9.86 |
| Inv10001 | Ean222 | 4.00 | 9.86 |
+---------------+--------+------+-----------+
像这样:
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 名称空间并将目标节点引用为 /soap:Envelope/soap:Body/GetInvoiceResult
.
我想解析下面的 @xml 并生成一个 table,例如:
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 并生成带有 header 和行的 table。
+---------------+--------+------+-----------+
| InvoiceNumber | Ean | Cost | TotalCost |
+---------------+--------+------+-----------+
| Inv10001 | Ean111 | 5.86 | 9.86 |
| Inv10001 | Ean222 | 4.00 | 9.86 |
+---------------+--------+------+-----------+
像这样:
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 名称空间并将目标节点引用为 /soap:Envelope/soap:Body/GetInvoiceResult
.