我试图以通常的方式查询以下XML,但是由于其声明以及空的“ NextQuantity”-“ NextDate”字段,我在查询XML时遇到了麻烦。
什么是正确的方法?
谢谢。
<StocksResp xmlns="http://xxxxxx" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Stocks>
<Stock>
<Sku>30101.06-L</Sku>
<Quantity>247610</Quantity>
<NextQuantity1>15243</NextQuantity1>
<NextDate1>2019-02-27</NextDate1>
<NextQuantity2 i:nil="true" />
<NextDate2 />
<NextQuantity3 i:nil="true" />
<NextDate3 />
<NextQuantity4 i:nil="true" />
<NextDate4 />
<NextQuantity5 i:nil="true" />
<NextDate5 />
<NextQuantity6 i:nil="true" />
<NextDate6 />
</Stock>
<Stock>
<Sku>30101.06-M</Sku>
<Quantity>241606</Quantity>
<NextQuantity1 i:nil="true" />
<NextDate1 />
<NextQuantity2 i:nil="true" />
<NextDate2 />
<NextQuantity3 i:nil="true" />
<NextDate3 />
<NextQuantity4 i:nil="true" />
<NextDate4 />
<NextQuantity5 i:nil="true" />
<NextDate5 />
<NextQuantity6 i:nil="true" />
<NextDate6 />
</Stock>
</Stocks>
<Count>4837</Count>
<Currency i:nil="true" />
<Language>ES</Language>
<ErrorCode i:nil="true" />
<ErrorMessage i:nil="true" />
</StocksResp>
================================================
DECLARE @xml XML
SELECT @xml = x.y FROM OPENROWSET (BULK 'file.xml',SINGLE_BLOB) as x(y)
SELECT
x.y.value('Sku[1]','varchar(15)'),
x.y.value('Quantity[1]','int'),
x.y.value('NextDate1[1]','varchar(20)'),
x.y.value('NextQuantity1[1]','int'),
'mr'
FROM @xml.nodes('Stocks/Stock') x(y)
答案 0 :(得分:0)
可以提供帮助吗?
DECLARE @xml XML=
N'<StocksResp xmlns="http://xxxxxx" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Stocks>
<Stock>
<Sku>30101.06-L</Sku>
<Quantity>247610</Quantity>
<NextQuantity1>15243</NextQuantity1>
<NextDate1>2019-02-27</NextDate1>
<NextQuantity2 i:nil="true" />
<NextDate2 />
<NextQuantity3 i:nil="true" />
<NextDate3 />
<NextQuantity4 i:nil="true" />
<NextDate4 />
<NextQuantity5 i:nil="true" />
<NextDate5 />
<NextQuantity6 i:nil="true" />
<NextDate6 />
</Stock>
<Stock>
<Sku>30101.06-M</Sku>
<Quantity>241606</Quantity>
<NextQuantity1 i:nil="true" />
<NextDate1 />
<NextQuantity2 i:nil="true" />
<NextDate2 />
<NextQuantity3 i:nil="true" />
<NextDate3 />
<NextQuantity4 i:nil="true" />
<NextDate4 />
<NextQuantity5 i:nil="true" />
<NextDate5 />
<NextQuantity6 i:nil="true" />
<NextDate6 />
</Stock>
</Stocks>
<Count>4837</Count>
<Currency i:nil="true" />
<Language>ES</Language>
<ErrorCode i:nil="true" />
<ErrorMessage i:nil="true" />
</StocksResp>';
-您需要声明默认名称空间。您无需声明名称空间i
...
WITH XMLNAMESPACES(DEFAULT 'http://xxxxxx','http://www.w3.org/2001/XMLSchema-instance' AS i)
SELECT
@xml.value(N'(/StocksResp/Count/text())[1]',N'int') AS StockResp_Count
,@xml.value(N'(/StocksResp/Currency/text())[1]',N'int') AS StockResp_Currency
,st.value(N'(Sku/text())[1]',N'nvarchar(max)') AS Stock_Sku
,st.value(N'(Quantity/text())[1]',N'int') AS Stock_Quantity
,st.value(N'(NextQuantity1/text())[1]',N'int') AS Stock_NextQuantity1
,st.value(N'(NextDate1/text())[1]',N'date') AS Stock_NextDate1
,st.value(N'(NextQuantity2/text())[1]',N'int') AS Stock_NextQuantity2
,st.value(N'(NextDate2/text())[1]',N'date') AS Stock_NextDate2
,st.value(N'(NextQuantity3/text())[1]',N'int') AS Stock_NextQuantity3
,st.value(N'(NextDate3/text())[1]',N'date') AS Stock_NextDate3
,st.value(N'(NextQuantity4/text())[1]',N'int') AS Stock_NextQuantity4
,st.value(N'(NextDate4/text())[1]',N'date') AS Stock_NextDate4
,st.value(N'(NextQuantity5/text())[1]',N'int') AS Stock_NextQuantity5
,st.value(N'(NextDate5/text())[1]',N'date') AS Stock_NextDate5
,st.value(N'(NextQuantity6/text())[1]',N'int') AS Stock_NextQuantity6
,st.value(N'(NextDate6/text())[1]',N'date') AS Stock_NextDate6
FROM @xml.nodes('/StocksResp/Stocks/Stock') A(st);
一些评论:
使用 name-number 元素(Date1,Date2等)是一个非常不好的习惯。如果可以更改此设置,则应使用一组嵌套元素,并根据它们的位置或诸如
之类的属性对其进行编号。<Next index="1">
<Date>2018-01-01</Date>
<Quantity>1</Quantity>
</Next>
[... more of them ...]
您可以看到,我直接从变量中读取了一次值,而重复元素是由.nodes()
从派生表中获取的。