如果我有两种不同类型的路径来处理信息,我如何使用oracle extractvalue函数选择xml数据?
对于年份和品牌,我可以使用以下方式恢复信息:
extractvalue(xmltype(x.xml, 0), '//Vehicle/Year') as VEC_YEAR,
但是对于地址我有错误消息:ORA31011无法解析xml和ORA19202和LPX00601:无效令牌://品牌/信息/数据/地址
SELECT
extractvalue(xmltype(x.xml, 0), '//Vehicle/Year') as VEC_YEAR,
extractvalue(xmltype(x.xml, 0), '//Vehicle/Brand') as VEC_BRAND,
CASE
WHEN extractvalue(xmltype(x.xml, 0), '//Brand/Info/Data/Address') IS NOT NULL THEN extractvalue(xmltype(x.xml, 0), '//Brand/Info/Data/Address')
WHEN extractvalue(xmltype(x.xml, 0), '//Brand/MainInfo/Info/Data/Address') IS NOT NULL THEN extractvalue(xmltype(x.xml, 0), '//Brand/MainInfo/Info/Data/Address')
END as Adress,
FROM xml_table x
这些是我必须查询的XML:
<?xml version="1.0" encoding="utf-8"?>
<VehicleValidation xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Validation Id="Validation">
<Vehicle>
<Year>2017</Year>
<Brand>One car brand</Brand>
</Vehicle>
<Brand>
<Info>
<Data>
<Address>
One car brand Address
</Address>
<Data>
</Info>
</Brand>
</Validation>
</VehicleValidation>
<?xml version="1.0" encoding="utf-8"?>
<VehicleValidation xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Validation Id="Validation">
<Vehicle>
<Year>2017</Year>
<Brand>One car brand</Brand>
</Vehicle>
<Brand>
<MainInfo>
<Info>
<Data>
<Address>
One car brand Address
</Address>
<Data>
</Info>
</MainInfo>
</Brand>
</Validation>
</VehicleValidation>
答案 0 :(得分:1)
如何在品牌下方获取任何信息/数据/地址? 即使用XPath //品牌//信息/数据/地址而不是//品牌/信息/数据/地址和//品牌/主要信息/信息/数据/地址 https://www.w3.org/TR/xpath/#path-abbrev
如果您需要准确,请尝试在when条件中使用existsnode而不是extractvalue https://docs.oracle.com/cd/B12037_01/appdev.101/b10790/xdb04cre.htm#i1032763
顺便说一句,在发布的xml中,Data标签没有正确关闭。
答案 1 :(得分:1)
这是一个有效的例子..注意事项。请尽量不要使用extractValue()。它已被弃用。您应该使用XML标准定义的SQL / XML运算符。要从XML文档中提取关系行,XMLTABLE应该是首选运算符..
SQL> with MY_TABLE as
2 (
3 select XMLTYPE(
4 '<?xml version="1.0" encoding="utf-8"?>
5 <VehicleValidation xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
6 <Validation Id="Validation">
7 <Vehicle>
8 <Year>2017</Year>
9 <Brand>One car brand</Brand>
10 </Vehicle>
11 <Brand>
12 <Info>
13 <Data>
14 <Address>
15 One car brand Address
16 </Address>
17 </Data>
18 </Info>
19 </Brand>
20 </Validation>
21 </VehicleValidation>') as XMLDOC
22 from DUAL
23 union all
24 select XMLTYPE(
25 '<?xml version="1.0" encoding="utf-8"?>
26 <VehicleValidation xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
27 <Validation Id="Validation">
28 <Vehicle>
29 <Year>2017</Year>
30 <Brand>One car brand</Brand>
31 </Vehicle>
32 <Brand>
33 <MainInfo>
34 <Info>
35 <Data>
36 <Address>
37 One car brand Address
38 </Address>
39 </Data>
40 </Info>
41 </MainInfo>
42 </Brand>
43 </Validation>
44 </VehicleValidation>') as XMLDOC
45 from dual
46 )
47 select Brand, Address
48 from MY_TABLE,
49 XMLTABLE(
50 '/VehicleValidation/Validation'
51 passing XMLDOC
52 COLUMNS
53 BRAND VARCHAR2(32) PATH 'Vehicle/Brand',
54 ADDRESS VARCHAR2(64) PATH 'Brand//Info/Data/Address'
55 )
56 /
BRAND ADDRESS
-------------------------------- ----------------------------------------------------------------
One car brand
One car brand Address
One car brand
One car brand Address