[英]How to parse an XML in SQL when the structure of the file is changing?
I have the following XML structure in a column ( that is just a small portion of it): 我在列中有以下XML结构(仅是其中的一小部分):
<block name="decision">
<subdictionary name="main">
<v id="Profit">126.45</v>
<v id="Check">-99999.00</v>
<v id="RulePath">PD159</v>
<v id="ID">3256423</v>
<v id="Outcome">RejectFinal</v>
<v id="RP">,PD159</v>
</subdictionary>
</block>
What I am trying to do is parse the result for "Outcome", but the problem for me is that for each different xml file the postion of "Outcome" can change. 我想做的是解析“结果”的结果,但是对我来说,问题是对于每个不同的xml文件,“结果”的位置都可以更改。 What I have been trying so far is this
到目前为止,我一直在尝试的是
SELECT
a.XmlResponse.value('(/decisiondocument/block[3]/subdictionary[1]/v)[@Outcome]', 'nvarchar(20)') 'Outcome'
FROM table
but it gives the following error: 但它给出以下错误:
XQuery [Table_Request.XmlResponse.value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
XQuery [Table_Request.XmlResponse.value()]:“ value()”需要一个单例(或空序列),找到类型为“ xdt:untypedAtomic *”的操作数
The desired result will be something like this: 期望的结果将是这样的:
Column Outcome
1 RejectFinal
2 Approved
3 RejectFinal
Use this xpath. 使用此xpath。 It'll match all
v
elements where id
attribute matches "Outcome": 它将匹配
id
属性与“结果”匹配的所有v
元素:
SELECT
a.XmlResponse.value('(//v[@id="Outcome"])[1]', 'varchar(100)')
FROM t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.