如何让 PL/SQL 解析 Oracle 中的 XML 属性而不是 XML 元素?
请参阅下面的 PL/SQL 片段:
create table t23 (
field01 number,
field02 number,
field03 char(1)
);
示例片段 #1
declare x varchar2(2000) := '
<ArrayOfRecords>
<Record>
<Field01>130</Field01>
<Field02>1700</Field02>
<Field03>C</Field03>
</Record>
<Record>
<Field01>131</Field01>
<Field02>1701</Field02>
<Field03>B</Field03>
</Record>
</ArrayOfRecords>';
begin
insert
into T23
SELECT *
FROM XMLTABLE('/ArrayOfRecords/Record'
PASSING xmltype(x)
COLUMNS
Field01 number PATH 'Field01',
Field02 number PATH 'Field02',
Field03 char(1) PATH 'Field03'
);
end;
-- The records will be here - great!
select * from T23;
示例片段 #2
declare y varchar2(2000) := '
<ArrayOfRecords>
<Record Field01="130" Field02="1700" Field03="C" />
<Record Field01="131" Field02="1701" Field03="B" />
</ArrayOfRecords>';
begin
insert
into T23
SELECT *
FROM XMLTABLE('/ArrayOfRecords/Record'
PASSING xmltype(y)
COLUMNS
Field01 number PATH 'Field01',
Field02 number PATH 'Field02',
Field03 char(1) PATH 'Field03'
);
end;
-- null values are inserted into the table - not great
select * from T23;
如何从示例片段 #2 中的 XML 属性中获取值?有没有办法告诉 xmltype 从属性和元素中检索数据?
Please see PL/SQL snippets below:
create table t23 (
field01 number,
field02 number,
field03 char(1)
);
Example Snippet #1
declare x varchar2(2000) := '
<ArrayOfRecords>
<Record>
<Field01>130</Field01>
<Field02>1700</Field02>
<Field03>C</Field03>
</Record>
<Record>
<Field01>131</Field01>
<Field02>1701</Field02>
<Field03>B</Field03>
</Record>
</ArrayOfRecords>';
begin
insert
into T23
SELECT *
FROM XMLTABLE('/ArrayOfRecords/Record'
PASSING xmltype(x)
COLUMNS
Field01 number PATH 'Field01',
Field02 number PATH 'Field02',
Field03 char(1) PATH 'Field03'
);
end;
-- The records will be here - great!
select * from T23;
Example Snippet #2
declare y varchar2(2000) := '
<ArrayOfRecords>
<Record Field01="130" Field02="1700" Field03="C" />
<Record Field01="131" Field02="1701" Field03="B" />
</ArrayOfRecords>';
begin
insert
into T23
SELECT *
FROM XMLTABLE('/ArrayOfRecords/Record'
PASSING xmltype(y)
COLUMNS
Field01 number PATH 'Field01',
Field02 number PATH 'Field02',
Field03 char(1) PATH 'Field03'
);
end;
-- null values are inserted into the table - not great
select * from T23;
How do you get the values out of the XML attributes in example snippet #2? Is there a way to tell xmltype to retrieve data from attributes vs. elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对属性使用
@
语法Use the
@
syntax for attributes