如何 select 匹配 PLSQL 中的值的子 XML?
How to select a sub XML that matches a value in PLSQL?
我有一些XML喜欢
<?xml version="1.0" encoding="utf-8"?>
<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>B</name><age>34</age><type>Temporary</type></employee>
<employee><name>C</name><age>63</age><type>Temporary</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
<company><cname>Company A</cname></company>
<company><cname>Company B</cname></company>
<company><cname>Company C</cname></company>
</response>
我只想要 "Permanent" 员工详细信息和 header 作为输出。像,
<?xml version="1.0" encoding="utf-8"?>
<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
</response>
有没有办法使用 PL/SQL 获得这种类型的输出?
提前致谢。
declare
v_xml xmltype := xmltype('<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>B</name><age>34</age><type>Temporary</type></employee>
<employee><name>C</name><age>63</age><type>Temporary</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
<company><cname>Company A</cname></company>
<company><cname>Company B</cname></company>
<company><cname>Company C</cname></company>
</response>');
begin
dbms_output.put_line('1) Extract only Permanent employee.');
dbms_output.put_line(v_xml.extract('//response/employee[type !="Permanent"]').getClobVal());
dbms_output.put_line(' ');
dbms_output.put_line('2) Remove unnecessary node.');
dbms_output.put_line( v_xml.deleteXML('//response/employee[type !="Permanent"]').deleteXML('//response/company').getStringVal());
end;
要仅提取长期雇员,您可以使用这个简单的 select:
SELECT
xml_tab.*
FROM
XMLTABLE(
'/response/employee' PASSING XMLTYPE(q'#<?xml version="1.0" encoding="utf-8"?>
<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>B</name><age>34</age><type>Temporary</type></employee>
<employee><name>C</name><age>63</age><type>Temporary</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
<company><cname>Company A</cname></company>
<company><cname>Company B</cname></company>
<company><cname>Company C</cname></company>
</response>#')
COLUMNS
i FOR ORDINALITY
,name VARCHAR2(100) PATH 'name'
,age VARCHAR2(3) PATH 'age'
,type VARCHAR2(100) PATH 'type'
) xml_tab
WHERE
xml_tab.type = 'Permanent';
这会产生以下输出:
I |NAME|AGE | TYPE
---------------------
1 | A | 32 | Permanent
4 | D | 64 | Permanent
5 | E | 54 | Permanent
我有一些XML喜欢
<?xml version="1.0" encoding="utf-8"?>
<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>B</name><age>34</age><type>Temporary</type></employee>
<employee><name>C</name><age>63</age><type>Temporary</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
<company><cname>Company A</cname></company>
<company><cname>Company B</cname></company>
<company><cname>Company C</cname></company>
</response>
我只想要 "Permanent" 员工详细信息和 header 作为输出。像,
<?xml version="1.0" encoding="utf-8"?>
<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
</response>
有没有办法使用 PL/SQL 获得这种类型的输出?
提前致谢。
declare
v_xml xmltype := xmltype('<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>B</name><age>34</age><type>Temporary</type></employee>
<employee><name>C</name><age>63</age><type>Temporary</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
<company><cname>Company A</cname></company>
<company><cname>Company B</cname></company>
<company><cname>Company C</cname></company>
</response>');
begin
dbms_output.put_line('1) Extract only Permanent employee.');
dbms_output.put_line(v_xml.extract('//response/employee[type !="Permanent"]').getClobVal());
dbms_output.put_line(' ');
dbms_output.put_line('2) Remove unnecessary node.');
dbms_output.put_line( v_xml.deleteXML('//response/employee[type !="Permanent"]').deleteXML('//response/company').getStringVal());
end;
要仅提取长期雇员,您可以使用这个简单的 select:
SELECT
xml_tab.*
FROM
XMLTABLE(
'/response/employee' PASSING XMLTYPE(q'#<?xml version="1.0" encoding="utf-8"?>
<response>
<HEADER>some content</HEADER>
<employee><name>A</name><age>32</age><type>Permanent</type></employee>
<employee><name>B</name><age>34</age><type>Temporary</type></employee>
<employee><name>C</name><age>63</age><type>Temporary</type></employee>
<employee><name>D</name><age>64</age><type>Permanent</type></employee>
<employee><name>E</name><age>54</age><type>Permanent</type></employee>
<company><cname>Company A</cname></company>
<company><cname>Company B</cname></company>
<company><cname>Company C</cname></company>
</response>#')
COLUMNS
i FOR ORDINALITY
,name VARCHAR2(100) PATH 'name'
,age VARCHAR2(3) PATH 'age'
,type VARCHAR2(100) PATH 'type'
) xml_tab
WHERE
xml_tab.type = 'Permanent';
这会产生以下输出:
I |NAME|AGE | TYPE
---------------------
1 | A | 32 | Permanent
4 | D | 64 | Permanent
5 | E | 54 | Permanent