简体   繁体   中英

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):

<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. 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 *'

The desired result will be something like this:

 Column    Outcome
    1      RejectFinal
    2      Approved
    3      RejectFinal

Use this xpath. It'll match all v elements where id attribute matches "Outcome":

SELECT
    a.XmlResponse.value('(//v[@id="Outcome"])[1]', 'varchar(100)')
FROM t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM