我試圖從這個 xml 中提取三個
列印機位置,列印機位置存盤在列印機/列印機頭/元素下,屬性名稱為列印機位置
問題是如何在“USA,UK,(null)”之
類的串列中獲取結果,或者以我可以這樣查詢的列格式獲取結果:
select * from ( this xml query) where column1 = "USA"
這是一個示例 xml:
<?xml version="1.0" encoding="UTF16" standalone="no" ?>
<printer>
<printer-header>
<elem name="printerId">XROX101-19341</elem>
<elem name="printerDate">05/11/19 12:27:48</elem>
<elem name="printerLocations">
<elem name="countryCd">USA</elem>
</elem>
<elem name="printerLocations">
<elem name="countryCd">UK</elem>
</elem>
<elem name="printerLocations">
<elem name="countryCd"/>
</elem>
</printer-header>
</printer>
我試過的:
select XMLTYPE(xml_string).extract('/printer/printer-header/elem[@name="printerLocations"]/elem[@name="countryCd"]/text()').getStringVal() from printers
/
這只是一個刺痛價值美國
我希望得到的輸出是一個包含三列的表格,
COUNTRY1 COUNTRY2 COUNTRY3
USA UK NULL
uj5u.com熱心網友回復:
您可以使用 XMLTable 提取多個值,包括父元素值,例如:
select printerId, printerDate, countryCd
from xmltable(
'/printer/printer-header/elem[@name="printerLocations"]'
passing xmltype('<?xml version="1.0" encoding="UTF16" standalone="no" ?>
<printer>
<printer-header>
<elem name="printerId">XROX101-19341</elem>
<elem name="printerDate">05/11/19 12:27:48</elem>
<elem name="printerLocations">
<elem name="countryCd">USA</elem>
</elem>
<elem name="printerLocations">
<elem name="countryCd">UK</elem>
</elem>
<elem name="printerLocations">
<elem name="countryCd"/>
</elem>
</printer-header>
</printer>')
columns
printerId varchar2(30) path './../elem[@name="printerId"]',
printerDate varchar2(17) path './../elem[@name="printerDate"]',
countryCd varchar2(3) path 'elem[@name="countryCd"]'
)
| 列印機ID | 列印機日期 | 鄉村唱片 |
|---|---|---|
| XROX101-19341 | 05/11/19 12:27:48 | 美國 |
| XROX101-19341 | 05/11/19 12:27:48 | 英國 |
| XROX101-19341 | 05/11/19 12:27:48 | 空值 |
您可以將 'printDate' 值轉換為實際日期,假設它是固定格式。
無論哪種方式,您都可以將其用作行內視圖或 CTE。
資料庫<>小提琴
我試過的: select XMLTYPE(xml_string).extract('/printer/printer-header/elem[@name="printerLocations"]/elem[@name="countryCd"]/text()').getStringVal() from列印機
這會將所有國家/地區值作為單個字串;使用 XMLTable 可以分別獲取每個值,并允許您輕松獲取其他資料,以及一次獲取多個 XML 檔案的資料。
由于您的字串來自表格,因此變為:
select x.printerId, x.printerDate, x.countryCd
from printers p
cross apply xmltable(
'/printer/printer-header/elem[@name="printerLocations"]'
passing xmltype(p.xml_string)
columns
printerId varchar2(30) path './../elem[@name="printerId"]',
printerDate varchar2(17) path './../elem[@name="printerDate"]',
countryCd varchar2(3) path 'elem[@name="countryCd"]'
) x
| 列印機ID | 列印機日期 | 鄉村唱片 |
|---|---|---|
| XROX101-19341 | 05/11/19 12:27:48 | 美國 |
| XROX101-19341 | 05/11/19 12:27:48 | 英國 |
| XROX101-19341 | 05/11/19 12:27:48 | 空值 |
它包含在外部查詢中,轉換日期為:
select * from (
select
printerId,
to_date(printerDate, 'DD/MM/RR HH24:MI:SS') as printerDate,
countryCd
from printers p
cross apply xmltable(
'/printer/printer-header/elem[@name="printerLocations"]'
passing xmltype(p.xml_string)
columns
printerId varchar2(30) path './../elem[@name="printerId"]',
printerDate varchar2(17) path './../elem[@name="printerDate"]',
countryCd varchar2(3) path 'elem[@name="countryCd"]'
)
) x
where x.countryCd = 'USA'
| 列印機ID | 列印機日期 | 鄉村唱片 |
|---|---|---|
| XROX101-19341 | 2019-11-05 12:27:48 | 美國 |
資料庫<>小提琴
我只需要一張三列的表格
你可以調整這個結果;但更簡單的是,您可以使用類似的方法,按索引定位每個列印機位置:
select x.countryCd1, x.countryCd2, x.countryCd3
from printers p
cross apply xmltable(
'/printer/printer-header'
passing xmltype(p.xml_string)
columns
countryCd1 varchar2(3) path 'elem[@name="printerLocations"][1]/elem[@name="countryCd"]',
countryCd2 varchar2(3) path 'elem[@name="printerLocations"][2]/elem[@name="countryCd"]',
countryCd3 varchar2(3) path 'elem[@name="printerLocations"][3]/elem[@name="countryCd"]'
) x
| 國家CD1 | 國家CD2 | 國家CD3 |
|---|---|---|
| 美國 | 英國 | 空值 |
資料庫<>小提琴
您可以改用三個 XMLQuery 呼叫,但這會涉及更多的重復。
或者,如果您真的想要,您仍然可以使用原始提取物三次,并添加索引:
select
XMLTYPE(xml_string).extract('/printer/printer-header/elem[@name="printerLocations"][1]/elem[@name="countryCd"]/text()').getStringVal() as countryCd1,
XMLTYPE(xml_string).extract('/printer/printer-header/elem[@name="printerLocations"][2]/elem[@name="countryCd"]/text()').getStringVal() as countryCd2,
XMLTYPE(xml_string).extract('/printer/printer-header/elem[@name="printerLocations"][3]/elem[@name="countryCd"]/text()').getStringVal() as countryCd3
from printers
...得到相同的結果。
無論哪種方式,您都必須事先知道最終結果中有多少個值(列)。(否則你必須使用動態 SQL...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/388835.html
