我無法獲得col1、col2、col3的值。我做錯了什么? 我在Postgres中使用的是XML的第一個計時器。
另外還添加了結果XML和SQL Server腳本。
是否有可能得到與SQL Server中相同的結果
我添加了更多的細節,并修正了問題中的錯誤。我添加了更多的細節,并修正了問題中的錯誤。
with cte as (SELECT
'<a id_file="9965_99" version="4。 1.0">
<b>
<bbb name_code="note"/>
</b>/span>
<c id="d000fe02-bd80-46ac-8d60-afdf3e194f90"/span>>
<f ddd="test1"/span> />
<r rr="1" />/span>
</c>
<c id="d000fe02-bd80-46ac-8d60-afdf3e194f91"/span>>
<f ddd="test2"/span> />
<r rr="2" />/span>
</c>
<c id="d0t0fe02-bd80-46ac-8d60-afdf3e194f92"/span>>
<f ddd="test3"/span> />
<r rr="3" />/span>
</c>
</a> ' :xml)
選擇
(xpath('//@id_file', xt))[1] 作為col1。
(xpath('//@version', xt))[1]作為col2。
(xpath('//b/@bbb', xt))[1] 作為col3。
(xpath('//bbb/@name_code', xt))[1] 作為col7。
(xpath('@id', xt))[1]作為col4。
(xpath('f/@ddd', xt))[1] 作為col5。
(xpath('r/@rr', xt))[1] 作為col6
從cte
cross join unnest(xpath('*', xml)) as xt;
Postgres結果:

SQL服務器結果:

下面是SQL服務器的腳本
DECLARE @xml XML。
設定 @xml =
'
<a id_file="9965_99" version="4。 1.0">
<b>
<bbb name_code="note"/>
</b>/span>
<c id="d000fe02-bd80-46ac-8d60-afdf3e194f90"/span>>
<f ddd="test1"/span> />
<r rr="1" />/span>
</c>
<c id="d000fe02-bd80-46ac-8d60-afdf3e194f91"/span>>
<f ddd="test2"/span> />
<r rr="2" />/span>
</c>
<c id="d0t0fe02-bd80-46ac-8d60-afdf3e194f92"/span>>
<f ddd="test3"/span> />
<r rr="3" />/span>
</c>
</a>
'
選擇
T2.Loc.value('(//@id_file)[1]', 'varchar(100)') AS [id_file],
T2.Loc.value('(//@version)[1]', 'varchar(100)') AS [version],
T2.Loc.value('(//bbb/@name_code)[1]', 'varchar(100)') AS [name_code],
T2.Loc.value('(@id)[1]', 'varchar(500)') AS [id],
T2.Loc.value('(f/@ddd)[1]', 'varchar(500)' ) AS [ddd],
T2.Loc.value('(r/@rrr)[1]', 'varchar(500)' ) AS [rrr]
FROM @xml.nodes('//c') as T2(Loc)
uj5u.com熱心網友回復:
我發現使用xmltable()來做這個更容易:
with cte (doc) as (SELECT)
'<a id_file="9965_99" version="4.1.0" >
<b>
<bbb name_code="note"/>
</b>
<c id="d000fe02-bd80-46ac-8d60-afdf3e194f90">
<f ddd="test1" />
<r rrr="1" />
</c>
<c id="d000fe02-bd80-46ac-8d60-afdf3e194f91">
<f ddd="test2" />
<r rrr="2" />
</c>
<c id="d0t0fe02-bd80-46ac-8d60-afdf3e194f92">
<f ddd="test3" />
<r rrr="3" />
</c>
</a>':xml)
SELECT xt.*
from cte
cross join xmltable('/a/c' pass doc)
列
col1 text path './@id_file',
col2 text path './@version',
col3 text path './b/bbb/@name_code',
col4 text path '@id',
col5 text path 'f/@ddd',
col6 text path 'r/@rr') as xt
;
由于你希望每個<c>標簽有一條記錄,你需要在xmltable()中使用它作為起始路徑。所以每一列的xpath運算式需要相對于c標簽。如果你不想硬編碼.../b/bbb路徑,你可以選擇使用'//bbb/@name_code'/p>。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332446.html
標籤:
