5 INSERT INTO xmltest VALUES (1, '<value>one</value>');
6 INSERT INTO xmltest VALUES (2, '<value>two</value>');
7 INSERT INTO xmltest VALUES (3, '<wrong');
8 ERROR: invalid XML content
9 LINE 1: INSERT INTO xmltest VALUES (3, '<wrong');
11 DETAIL: line 1: Couldn't find end of Start Tag wrong line 1
12 SELECT * FROM xmltest;
14 ----+--------------------
15 1 | <value>one</value>
16 2 | <value>two</value>
19 SELECT xmlcomment('test');
25 SELECT xmlcomment('-test');
31 SELECT xmlcomment('test-');
32 ERROR: invalid XML comment
33 SELECT xmlcomment('--test');
34 ERROR: invalid XML comment
35 SELECT xmlcomment('te st');
41 SELECT xmlconcat(xmlcomment('hello'),
42 xmlelement(NAME qux, 'foo'),
45 ----------------------------------------
46 <!--hello--><qux>foo</qux><!--world-->
49 SELECT xmlconcat('hello', 'you');
55 SELECT xmlconcat(1, 2);
56 ERROR: argument of XMLCONCAT must be type xml, not type integer
57 LINE 1: SELECT xmlconcat(1, 2);
59 SELECT xmlconcat('bad', '<syntax');
60 ERROR: invalid XML content
61 LINE 1: SELECT xmlconcat('bad', '<syntax');
63 DETAIL: line 1: Couldn't find end of Start Tag syntax line 1
64 SELECT xmlconcat('<foo/>', NULL, '<?xml version="1.1" standalone="no"?><bar/>');
70 SELECT xmlconcat('<?xml version="1.1"?><foo/>', NULL, '<?xml version="1.1" standalone="no"?><bar/>');
72 -----------------------------------
73 <?xml version="1.1"?><foo/><bar/>
76 SELECT xmlconcat(NULL);
82 SELECT xmlconcat(NULL, NULL);
88 SELECT xmlelement(name element,
89 xmlattributes (1 as one, 'deuce' as two),
92 ------------------------------------------------
93 <element one="1" two="deuce">content</element>
96 SELECT xmlelement(name element,
97 xmlattributes ('unnamed and wrong'));
98 ERROR: unnamed XML attribute value must be a column reference
99 LINE 2: xmlattributes ('unnamed and wrong'));
101 SELECT xmlelement(name element, xmlelement(name nested, 'stuff'));
103 -------------------------------------------
104 <element><nested>stuff</nested></element>
107 SELECT xmlelement(name employee, xmlforest(name, age, salary as pay)) FROM emp;
109 ----------------------------------------------------------------------
110 <employee><name>sharon</name><age>25</age><pay>1000</pay></employee>
111 <employee><name>sam</name><age>30</age><pay>2000</pay></employee>
112 <employee><name>bill</name><age>20</age><pay>1000</pay></employee>
113 <employee><name>jeff</name><age>23</age><pay>600</pay></employee>
114 <employee><name>cim</name><age>30</age><pay>400</pay></employee>
115 <employee><name>linda</name><age>19</age><pay>100</pay></employee>
118 SELECT xmlelement(name duplicate, xmlattributes(1 as a, 2 as b, 3 as a));
119 ERROR: XML attribute name "a" appears more than once
120 LINE 1: ...ment(name duplicate, xmlattributes(1 as a, 2 as b, 3 as a));
122 SELECT xmlelement(name num, 37);
128 SELECT xmlelement(name foo, text 'bar');
134 SELECT xmlelement(name foo, xml 'bar');
140 SELECT xmlelement(name foo, text 'b<a/>r');
142 -------------------------
143 <foo>b<a/>r</foo>
146 SELECT xmlelement(name foo, xml 'b<a/>r');
152 SELECT xmlelement(name foo, array[1, 2, 3]);
154 -------------------------------------------------------------------------
155 <foo><element>1</element><element>2</element><element>3</element></foo>
158 SET xmlbinary TO base64;
159 SELECT xmlelement(name foo, bytea 'bar');
165 SET xmlbinary TO hex;
166 SELECT xmlelement(name foo, bytea 'bar');
172 SELECT xmlelement(name foo, xmlattributes(true as bar));
178 SELECT xmlelement(name foo, xmlattributes('2009-04-09 00:24:37'::timestamp as bar));
180 ----------------------------------
181 <foo bar="2009-04-09T00:24:37"/>
184 SELECT xmlelement(name foo, xmlattributes('infinity'::timestamp as bar));
185 ERROR: timestamp out of range
186 DETAIL: XML does not support infinite timestamp values.
187 SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as funnier));
189 ------------------------------------------------------------
190 <foo funny="<>&"'" funnier="b<a/>r"/>
193 SELECT xmlparse(content '');
199 SELECT xmlparse(content ' ');
205 SELECT xmlparse(content 'abc');
211 SELECT xmlparse(content '<abc>x</abc>');
217 SELECT xmlparse(content '<invalidentity>&</invalidentity>');
218 ERROR: invalid XML content
219 DETAIL: line 1: xmlParseEntityRef: no name
220 <invalidentity>&</invalidentity>
222 line 1: chunk is not well balanced
223 SELECT xmlparse(content '<undefinedentity>&idontexist;</undefinedentity>');
224 ERROR: invalid XML content
225 DETAIL: line 1: Entity 'idontexist' not defined
226 <undefinedentity>&idontexist;</undefinedentity>
228 line 1: chunk is not well balanced
229 SELECT xmlparse(content '<invalidns xmlns=''<''/>');
231 ---------------------------
232 <invalidns xmlns='<'/>
235 SELECT xmlparse(content '<relativens xmlns=''relative''/>');
237 --------------------------------
238 <relativens xmlns='relative'/>
241 SELECT xmlparse(content '<twoerrors>&idontexist;</unbalanced>');
242 ERROR: invalid XML content
243 DETAIL: line 1: Entity 'idontexist' not defined
244 <twoerrors>&idontexist;</unbalanced>
246 line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
247 line 1: chunk is not well balanced
248 SELECT xmlparse(content '<nosuchprefix:tag/>');
250 ---------------------
254 SELECT xmlparse(document ' ');
255 ERROR: invalid XML document
256 DETAIL: line 1: Start tag expected, '<' not found
257 SELECT xmlparse(document 'abc');
258 ERROR: invalid XML document
259 DETAIL: line 1: Start tag expected, '<' not found
262 SELECT xmlparse(document '<abc>x</abc>');
268 SELECT xmlparse(document '<invalidentity>&</abc>');
269 ERROR: invalid XML document
270 DETAIL: line 1: xmlParseEntityRef: no name
271 <invalidentity>&</abc>
273 line 1: Opening and ending tag mismatch: invalidentity line 1 and abc
274 SELECT xmlparse(document '<undefinedentity>&idontexist;</abc>');
275 ERROR: invalid XML document
276 DETAIL: line 1: Entity 'idontexist' not defined
277 <undefinedentity>&idontexist;</abc>
279 line 1: Opening and ending tag mismatch: undefinedentity line 1 and abc
280 SELECT xmlparse(document '<invalidns xmlns=''<''/>');
282 ---------------------------
283 <invalidns xmlns='<'/>
286 SELECT xmlparse(document '<relativens xmlns=''relative''/>');
288 --------------------------------
289 <relativens xmlns='relative'/>
292 SELECT xmlparse(document '<twoerrors>&idontexist;</unbalanced>');
293 ERROR: invalid XML document
294 DETAIL: line 1: Entity 'idontexist' not defined
295 <twoerrors>&idontexist;</unbalanced>
297 line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
298 SELECT xmlparse(document '<nosuchprefix:tag/>');
300 ---------------------
304 SELECT xmlpi(name foo);
310 SELECT xmlpi(name xml);
311 ERROR: invalid XML processing instruction
312 DETAIL: XML processing instruction target name cannot be "xml".
313 SELECT xmlpi(name xmlstuff);
319 SELECT xmlpi(name foo, 'bar');
325 SELECT xmlpi(name foo, 'in?>valid');
326 ERROR: invalid XML processing instruction
327 DETAIL: XML processing instruction cannot contain "?>".
328 SELECT xmlpi(name foo, null);
334 SELECT xmlpi(name xml, null);
335 ERROR: invalid XML processing instruction
336 DETAIL: XML processing instruction target name cannot be "xml".
337 SELECT xmlpi(name xmlstuff, null);
343 SELECT xmlpi(name "xml-stylesheet", 'href="mystyle.css" type="text/css"');
345 -------------------------------------------------------
346 <?xml-stylesheet href="mystyle.css" type="text/css"?>
349 SELECT xmlpi(name foo, ' bar');
355 SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
361 SELECT xmlroot(xml '<foo/>', version '2.0');
363 -----------------------------
364 <?xml version="2.0"?><foo/>
367 SELECT xmlroot(xml '<foo/>', version no value, standalone yes);
369 ----------------------------------------------
370 <?xml version="1.0" standalone="yes"?><foo/>
373 SELECT xmlroot(xml '<?xml version="1.1"?><foo/>', version no value, standalone yes);
375 ----------------------------------------------
376 <?xml version="1.0" standalone="yes"?><foo/>
379 SELECT xmlroot(xmlroot(xml '<foo/>', version '1.0'), version '1.1', standalone no);
381 ---------------------------------------------
382 <?xml version="1.1" standalone="no"?><foo/>
385 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value, standalone no);
387 ---------------------------------------------
388 <?xml version="1.0" standalone="no"?><foo/>
391 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value, standalone no value);
397 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value);
399 ----------------------------------------------
400 <?xml version="1.0" standalone="yes"?><foo/>
419 ------------------------------------------------------------------------------------------
420 <?xml version="1.0" standalone="yes"?><gazonk name="val" num="2"><qux>foo</qux></gazonk>
423 SELECT xmlserialize(content data as character varying(20)) FROM xmltest;
430 SELECT xmlserialize(content 'good' as char(10));
436 SELECT xmlserialize(document 'bad' as text);
437 ERROR: not an XML document
438 SELECT xml '<foo>bar</foo>' IS DOCUMENT;
444 SELECT xml '<foo>bar</foo><bar>foo</bar>' IS DOCUMENT;
450 SELECT xml '<abc/>' IS NOT DOCUMENT;
456 SELECT xml 'abc' IS NOT DOCUMENT;
462 SELECT '<>' IS NOT DOCUMENT;
463 ERROR: invalid XML content
464 LINE 1: SELECT '<>' IS NOT DOCUMENT;
466 DETAIL: line 1: StartTag: invalid element name
469 SELECT xmlagg(data) FROM xmltest;
471 --------------------------------------
472 <value>one</value><value>two</value>
475 SELECT xmlagg(data) FROM xmltest WHERE id > 10;
481 SELECT xmlelement(name employees, xmlagg(xmlelement(name name, name))) FROM emp;
483 --------------------------------------------------------------------------------------------------------------------------------
484 <employees><name>sharon</name><name>sam</name><name>bill</name><name>jeff</name><name>cim</name><name>linda</name></employees>
487 -- Check mapping SQL identifier to XML name
488 SELECT xmlpi(name ":::_xml_abc135.%-&_");
490 -------------------------------------------------
491 <?_x003A_::_x005F_xml_abc135._x0025_-_x0026__?>
494 SELECT xmlpi(name "123");
500 PREPARE foo (xml) AS SELECT xmlconcat('<foo/>', $1);
501 SET XML OPTION DOCUMENT;
502 EXECUTE foo ('<bar/>');
509 ERROR: invalid XML document
510 LINE 1: EXECUTE foo ('bad');
512 DETAIL: line 1: Start tag expected, '<' not found
515 SELECT xml '<!DOCTYPE a><a/><b/>';
516 ERROR: invalid XML document
517 LINE 1: SELECT xml '<!DOCTYPE a><a/><b/>';
519 DETAIL: line 1: Extra content at the end of the document
522 SET XML OPTION CONTENT;
523 EXECUTE foo ('<bar/>');
529 EXECUTE foo ('good');
535 SELECT xml '<!-- in SQL:2006+ a doc is content too--> <?y z?> <!DOCTYPE a><a/>';
537 --------------------------------------------------------------------
538 <!-- in SQL:2006+ a doc is content too--> <?y z?> <!DOCTYPE a><a/>
541 SELECT xml '<?xml version="1.0"?> <!-- hi--> <!DOCTYPE a><a/>';
543 ------------------------------
544 <!-- hi--> <!DOCTYPE a><a/>
547 SELECT xml '<!DOCTYPE a><a/>';
553 SELECT xml '<!-- hi--> oops <!DOCTYPE a><a/>';
554 ERROR: invalid XML content
555 LINE 1: SELECT xml '<!-- hi--> oops <!DOCTYPE a><a/>';
557 DETAIL: line 1: StartTag: invalid element name
558 <!-- hi--> oops <!DOCTYPE a><a/>
560 SELECT xml '<!-- hi--> <oops/> <!DOCTYPE a><a/>';
561 ERROR: invalid XML content
562 LINE 1: SELECT xml '<!-- hi--> <oops/> <!DOCTYPE a><a/>';
564 DETAIL: line 1: StartTag: invalid element name
565 <!-- hi--> <oops/> <!DOCTYPE a><a/>
567 SELECT xml '<!DOCTYPE a><a/><b/>';
568 ERROR: invalid XML content
569 LINE 1: SELECT xml '<!DOCTYPE a><a/><b/>';
571 DETAIL: line 1: Extra content at the end of the document
574 -- Test backwards parsing
575 CREATE VIEW xmlview1 AS SELECT xmlcomment('test');
576 CREATE VIEW xmlview2 AS SELECT xmlconcat('hello', 'you');
577 CREATE VIEW xmlview3 AS SELECT xmlelement(name element, xmlattributes (1 as ":one:", 'deuce' as two), 'content&');
578 CREATE VIEW xmlview4 AS SELECT xmlelement(name employee, xmlforest(name, age, salary as pay)) FROM emp;
579 CREATE VIEW xmlview5 AS SELECT xmlparse(content '<abc>x</abc>');
580 CREATE VIEW xmlview6 AS SELECT xmlpi(name foo, 'bar');
581 CREATE VIEW xmlview7 AS SELECT xmlroot(xml '<foo/>', version no value, standalone yes);
582 CREATE VIEW xmlview8 AS SELECT xmlserialize(content 'good' as char(10));
583 CREATE VIEW xmlview9 AS SELECT xmlserialize(content 'good' as text);
584 SELECT table_name, view_definition FROM information_schema.views
585 WHERE table_name LIKE 'xmlview%' ORDER BY 1;
586 table_name | view_definition
587 ------------+-------------------------------------------------------------------------------------------------------------------
588 xmlview1 | SELECT xmlcomment('test'::text) AS xmlcomment;
589 xmlview2 | SELECT XMLCONCAT('hello'::xml, 'you'::xml) AS "xmlconcat";
590 xmlview3 | SELECT XMLELEMENT(NAME element, XMLATTRIBUTES(1 AS ":one:", 'deuce' AS two), 'content&') AS "xmlelement";
591 xmlview4 | SELECT XMLELEMENT(NAME employee, XMLFOREST(emp.name AS name, emp.age AS age, emp.salary AS pay)) AS "xmlelement"+
593 xmlview5 | SELECT XMLPARSE(CONTENT '<abc>x</abc>'::text STRIP WHITESPACE) AS "xmlparse";
594 xmlview6 | SELECT XMLPI(NAME foo, 'bar'::text) AS "xmlpi";
595 xmlview7 | SELECT XMLROOT('<foo/>'::xml, VERSION NO VALUE, STANDALONE YES) AS "xmlroot";
596 xmlview8 | SELECT (XMLSERIALIZE(CONTENT 'good'::xml AS character(10)))::character(10) AS "xmlserialize";
597 xmlview9 | SELECT XMLSERIALIZE(CONTENT 'good'::xml AS text) AS "xmlserialize";
600 -- Text XPath expressions evaluation
601 SELECT xpath('/value', data) FROM xmltest;
603 ----------------------
608 SELECT xpath(NULL, NULL) IS NULL FROM xmltest;
615 SELECT xpath('', '<!-- error -->');
616 ERROR: empty XPath expression
617 CONTEXT: SQL function "xpath" statement 1
618 SELECT xpath('//text()', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>');
624 SELECT xpath('//loc:piece/@id', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc', 'http://127.0.0.1']]);
630 SELECT xpath('//loc:piece', '<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc', 'http://127.0.0.1']]);
632 ------------------------------------------------------------------------------------------------------------------------------------------------
633 {"<local:piece xmlns:local=\"http://127.0.0.1\" id=\"1\">number one</local:piece>","<local:piece xmlns:local=\"http://127.0.0.1\" id=\"2\"/>"}
636 SELECT xpath('//loc:piece', '<local:data xmlns:local="http://127.0.0.1" xmlns="http://127.0.0.2"><local:piece id="1"><internal>number one</internal><internal2/></local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc', 'http://127.0.0.1']]);
638 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
639 {"<local:piece xmlns:local=\"http://127.0.0.1\" xmlns=\"http://127.0.0.2\" id=\"1\"><internal>number one</internal><internal2/></local:piece>","<local:piece xmlns:local=\"http://127.0.0.1\" id=\"2\"/>"}
642 SELECT xpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
644 -------------------------
645 {<b>two</b>,<b>etc</b>}
648 SELECT xpath('//text()', '<root><</root>');
654 SELECT xpath('//@value', '<root value="<"/>');
660 SELECT xpath('''<<invalid>>''', '<root/>');
662 ---------------------------
663 {<<invalid>>}
666 SELECT xpath('count(//*)', '<root><sub/><sub/></root>');
672 SELECT xpath('count(//*)=0', '<root><sub/><sub/></root>');
678 SELECT xpath('count(//*)=3', '<root><sub/><sub/></root>');
684 SELECT xpath('name(/*)', '<root><sub/><sub/></root>');
690 SELECT xpath('/nosuchtag', '<root/>');
696 SELECT xpath('root', '<root/>');
702 -- Round-trip non-ASCII data through xpath().
705 xml_declaration text := '<?xml version="1.0" encoding="ISO-8859-1"?>';
709 -- Per the documentation, except when the server encoding is UTF8, xpath()
710 -- may not work on non-ASCII data. The untranslatable_character and
711 -- undefined_function traps below, currently dead code, will become relevant
712 -- if we remove this limitation.
713 IF current_setting('server_encoding') <> 'UTF8' THEN
714 RAISE LOG 'skip: encoding % unsupported for xpath',
715 current_setting('server_encoding');
719 degree_symbol := convert_from('\xc2b0', 'UTF8');
720 res := xpath('text()', (xml_declaration ||
721 '<x>' || degree_symbol || '</x>')::xml);
722 IF degree_symbol <> res[1]::text THEN
723 RAISE 'expected % (%), got % (%)',
724 degree_symbol, convert_to(degree_symbol, 'UTF8'),
725 res[1], convert_to(res[1]::text, 'UTF8');
728 -- character with byte sequence 0xc2 0xb0 in encoding "UTF8" has no equivalent in encoding "LATIN8"
729 WHEN untranslatable_character
730 -- default conversion function for encoding "UTF8" to "MULE_INTERNAL" does not exist
731 OR undefined_function
732 -- unsupported XML feature
733 OR feature_not_supported THEN
734 RAISE LOG 'skip: %', SQLERRM;
737 -- Test xmlexists and xpath_exists
738 SELECT xmlexists('//town[text() = ''Toronto'']' PASSING BY REF '<towns><town>Bidford-on-Avon</town><town>Cwmbran</town><town>Bristol</town></towns>');
744 SELECT xmlexists('//town[text() = ''Cwmbran'']' PASSING BY REF '<towns><town>Bidford-on-Avon</town><town>Cwmbran</town><town>Bristol</town></towns>');
750 SELECT xmlexists('count(/nosuchtag)' PASSING BY REF '<root/>');
756 SELECT xpath_exists('//town[text() = ''Toronto'']','<towns><town>Bidford-on-Avon</town><town>Cwmbran</town><town>Bristol</town></towns>'::xml);
762 SELECT xpath_exists('//town[text() = ''Cwmbran'']','<towns><town>Bidford-on-Avon</town><town>Cwmbran</town><town>Bristol</town></towns>'::xml);
768 SELECT xpath_exists('count(/nosuchtag)', '<root/>'::xml);
774 INSERT INTO xmltest VALUES (4, '<menu><beers><name>Budvar</name><cost>free</cost><name>Carling</name><cost>lots</cost></beers></menu>'::xml);
775 INSERT INTO xmltest VALUES (5, '<menu><beers><name>Molson</name><cost>free</cost><name>Carling</name><cost>lots</cost></beers></menu>'::xml);
776 INSERT INTO xmltest VALUES (6, '<myns:menu xmlns:myns="http://myns.com"><myns:beers><myns:name>Budvar</myns:name><myns:cost>free</myns:cost><myns:name>Carling</myns:name><myns:cost>lots</myns:cost></myns:beers></myns:menu>'::xml);
777 INSERT INTO xmltest VALUES (7, '<myns:menu xmlns:myns="http://myns.com"><myns:beers><myns:name>Molson</myns:name><myns:cost>free</myns:cost><myns:name>Carling</myns:name><myns:cost>lots</myns:cost></myns:beers></myns:menu>'::xml);
778 SELECT COUNT(id) FROM xmltest WHERE xmlexists('/menu/beer' PASSING data);
784 SELECT COUNT(id) FROM xmltest WHERE xmlexists('/menu/beer' PASSING BY REF data BY REF);
790 SELECT COUNT(id) FROM xmltest WHERE xmlexists('/menu/beers' PASSING BY REF data);
796 SELECT COUNT(id) FROM xmltest WHERE xmlexists('/menu/beers/name[text() = ''Molson'']' PASSING BY REF data);
802 SELECT COUNT(id) FROM xmltest WHERE xpath_exists('/menu/beer',data);
808 SELECT COUNT(id) FROM xmltest WHERE xpath_exists('/menu/beers',data);
814 SELECT COUNT(id) FROM xmltest WHERE xpath_exists('/menu/beers/name[text() = ''Molson'']',data);
820 SELECT COUNT(id) FROM xmltest WHERE xpath_exists('/myns:menu/myns:beer',data,ARRAY[ARRAY['myns','http://myns.com']]);
826 SELECT COUNT(id) FROM xmltest WHERE xpath_exists('/myns:menu/myns:beers',data,ARRAY[ARRAY['myns','http://myns.com']]);
832 SELECT COUNT(id) FROM xmltest WHERE xpath_exists('/myns:menu/myns:beers/myns:name[text() = ''Molson'']',data,ARRAY[ARRAY['myns','http://myns.com']]);
838 CREATE TABLE query ( expr TEXT );
839 INSERT INTO query VALUES ('/menu/beers/cost[text() = ''lots'']');
840 SELECT COUNT(id) FROM xmltest, query WHERE xmlexists(expr PASSING BY REF data);
846 -- Test xml_is_well_formed and variants
847 SELECT xml_is_well_formed_document('<foo>bar</foo>');
848 xml_is_well_formed_document
849 -----------------------------
853 SELECT xml_is_well_formed_document('abc');
854 xml_is_well_formed_document
855 -----------------------------
859 SELECT xml_is_well_formed_content('<foo>bar</foo>');
860 xml_is_well_formed_content
861 ----------------------------
865 SELECT xml_is_well_formed_content('abc');
866 xml_is_well_formed_content
867 ----------------------------
871 SET xmloption TO DOCUMENT;
872 SELECT xml_is_well_formed('abc');
878 SELECT xml_is_well_formed('<>');
884 SELECT xml_is_well_formed('<abc/>');
890 SELECT xml_is_well_formed('<foo>bar</foo>');
896 SELECT xml_is_well_formed('<foo>bar</foo');
902 SELECT xml_is_well_formed('<foo><bar>baz</foo>');
908 SELECT xml_is_well_formed('<local:data xmlns:local="http://127.0.0.1"><local:piece id="1">number one</local:piece><local:piece id="2" /></local:data>');
914 SELECT xml_is_well_formed('<pg:foo xmlns:pg="http://postgresql.org/stuff">bar</my:foo>');
920 SELECT xml_is_well_formed('<pg:foo xmlns:pg="http://postgresql.org/stuff">bar</pg:foo>');
926 SELECT xml_is_well_formed('<invalidentity>&</abc>');
932 SELECT xml_is_well_formed('<undefinedentity>&idontexist;</abc>');
938 SELECT xml_is_well_formed('<invalidns xmlns=''<''/>');
944 SELECT xml_is_well_formed('<relativens xmlns=''relative''/>');
950 SELECT xml_is_well_formed('<twoerrors>&idontexist;</unbalanced>');
956 SET xmloption TO CONTENT;
957 SELECT xml_is_well_formed('abc');
963 -- Since xpath() deals with namespaces, it's a bit stricter about
964 -- what's well-formed and what's not. If we don't obey these rules
965 -- (i.e. ignore namespace-related errors from libxml), xpath()
966 -- fails in subtle ways. The following would for example produce
968 -- <invalidns xmlns='<'/>
969 -- which is invalid because '<' may not appear un-escaped in
971 -- Since different libxml versions emit slightly different
972 -- error messages, we suppress the DETAIL in this test.
974 SELECT xpath('/*', '<invalidns xmlns=''<''/>');
975 ERROR: could not parse XML document
976 \set VERBOSITY default
977 -- Again, the XML isn't well-formed for namespace purposes
978 SELECT xpath('/*', '<nosuchprefix:tag/>');
979 ERROR: could not parse XML document
980 DETAIL: line 1: Namespace prefix nosuchprefix on tag is not defined
983 CONTEXT: SQL function "xpath" statement 1
984 -- XPath deprecates relative namespaces, but they're not supposed to
985 -- throw an error, only a warning.
986 SELECT xpath('/*', '<relativens xmlns=''relative''/>');
987 WARNING: line 1: xmlns: URI relative is not absolute
988 <relativens xmlns='relative'/>
991 --------------------------------------
992 {"<relativens xmlns=\"relative\"/>"}
995 -- External entity references should not leak filesystem information.
996 SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/passwd">]><foo>&c;</foo>');
998 -----------------------------------------------------------------
999 <!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/passwd">]><foo>&c;</foo>
1002 SELECT XMLPARSE(DOCUMENT '<!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/no.such.file">]><foo>&c;</foo>');
1004 -----------------------------------------------------------------------
1005 <!DOCTYPE foo [<!ENTITY c SYSTEM "/etc/no.such.file">]><foo>&c;</foo>
1008 -- This might or might not load the requested DTD, but it mustn't throw error.
1009 SELECT XMLPARSE(DOCUMENT '<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"><chapter> </chapter>');
1011 ------------------------------------------------------------------------------------------------------------------------------------------------------
1012 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"><chapter> </chapter>
1016 CREATE TABLE xmldata(data xml);
1017 INSERT INTO xmldata VALUES('<ROWS>
1019 <COUNTRY_ID>AU</COUNTRY_ID>
1020 <COUNTRY_NAME>Australia</COUNTRY_NAME>
1021 <REGION_ID>3</REGION_ID>
1024 <COUNTRY_ID>CN</COUNTRY_ID>
1025 <COUNTRY_NAME>China</COUNTRY_NAME>
1026 <REGION_ID>3</REGION_ID>
1029 <COUNTRY_ID>HK</COUNTRY_ID>
1030 <COUNTRY_NAME>HongKong</COUNTRY_NAME>
1031 <REGION_ID>3</REGION_ID>
1034 <COUNTRY_ID>IN</COUNTRY_ID>
1035 <COUNTRY_NAME>India</COUNTRY_NAME>
1036 <REGION_ID>3</REGION_ID>
1039 <COUNTRY_ID>JP</COUNTRY_ID>
1040 <COUNTRY_NAME>Japan</COUNTRY_NAME>
1041 <REGION_ID>3</REGION_ID><PREMIER_NAME>Sinzo Abe</PREMIER_NAME>
1044 <COUNTRY_ID>SG</COUNTRY_ID>
1045 <COUNTRY_NAME>Singapore</COUNTRY_NAME>
1046 <REGION_ID>3</REGION_ID><SIZE unit="km">791</SIZE>
1049 -- XMLTABLE with columns
1051 FROM (SELECT data FROM xmldata) x,
1052 LATERAL XMLTABLE('/ROWS/ROW'
1054 COLUMNS id int PATH '@id',
1056 country_name text PATH 'COUNTRY_NAME/text()' NOT NULL,
1057 country_id text PATH 'COUNTRY_ID',
1058 region_id int PATH 'REGION_ID',
1059 size float PATH 'SIZE',
1060 unit text PATH 'SIZE/@unit',
1061 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
1062 id | _id | country_name | country_id | region_id | size | unit | premier_name
1063 ----+-----+--------------+------------+-----------+------+------+---------------
1064 1 | 1 | Australia | AU | 3 | | | not specified
1065 2 | 2 | China | CN | 3 | | | not specified
1066 3 | 3 | HongKong | HK | 3 | | | not specified
1067 4 | 4 | India | IN | 3 | | | not specified
1068 5 | 5 | Japan | JP | 3 | | | Sinzo Abe
1069 6 | 6 | Singapore | SG | 3 | 791 | km | not specified
1072 CREATE VIEW xmltableview1 AS SELECT xmltable.*
1073 FROM (SELECT data FROM xmldata) x,
1074 LATERAL XMLTABLE('/ROWS/ROW'
1076 COLUMNS id int PATH '@id',
1078 country_name text PATH 'COUNTRY_NAME/text()' NOT NULL,
1079 country_id text PATH 'COUNTRY_ID',
1080 region_id int PATH 'REGION_ID',
1081 size float PATH 'SIZE',
1082 unit text PATH 'SIZE/@unit',
1083 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
1084 SELECT * FROM xmltableview1;
1085 id | _id | country_name | country_id | region_id | size | unit | premier_name
1086 ----+-----+--------------+------------+-----------+------+------+---------------
1087 1 | 1 | Australia | AU | 3 | | | not specified
1088 2 | 2 | China | CN | 3 | | | not specified
1089 3 | 3 | HongKong | HK | 3 | | | not specified
1090 4 | 4 | India | IN | 3 | | | not specified
1091 5 | 5 | Japan | JP | 3 | | | Sinzo Abe
1092 6 | 6 | Singapore | SG | 3 | 791 | km | not specified
1096 CREATE OR REPLACE VIEW public.xmltableview1 AS
1097 SELECT "xmltable".id,
1099 "xmltable".country_name,
1100 "xmltable".country_id,
1101 "xmltable".region_id,
1104 "xmltable".premier_name
1105 FROM ( SELECT xmldata.data
1107 LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (x.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME/text()'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text))
1108 EXPLAIN (COSTS OFF) SELECT * FROM xmltableview1;
1110 -----------------------------------------
1112 -> Seq Scan on xmldata
1113 -> Table Function Scan on "xmltable"
1116 EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
1118 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1120 Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name
1121 -> Seq Scan on public.xmldata
1122 Output: xmldata.data
1123 -> Table Function Scan on "xmltable"
1124 Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name
1125 Table Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME/text()'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text))
1128 -- XMLNAMESPACES tests
1129 SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
1131 PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
1132 COLUMNS a int PATH 'zz:a');
1138 CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
1140 PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
1141 COLUMNS a int PATH 'zz:a');
1142 SELECT * FROM xmltableview2;
1148 SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
1150 PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
1151 COLUMNS a int PATH 'a');
1152 ERROR: DEFAULT namespace is not supported
1153 SELECT * FROM XMLTABLE('.'
1155 COLUMNS a text PATH 'foo/namespace::node()');
1157 --------------------------------------
1158 http://www.w3.org/XML/1998/namespace
1161 -- used in prepare statements
1164 FROM (SELECT data FROM xmldata) x,
1165 LATERAL XMLTABLE('/ROWS/ROW'
1167 COLUMNS id int PATH '@id',
1169 country_name text PATH 'COUNTRY_NAME' NOT NULL,
1170 country_id text PATH 'COUNTRY_ID',
1171 region_id int PATH 'REGION_ID',
1172 size float PATH 'SIZE',
1173 unit text PATH 'SIZE/@unit',
1174 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
1176 id | _id | country_name | country_id | region_id | size | unit | premier_name
1177 ----+-----+--------------+------------+-----------+------+------+---------------
1178 1 | 1 | Australia | AU | 3 | | | not specified
1179 2 | 2 | China | CN | 3 | | | not specified
1180 3 | 3 | HongKong | HK | 3 | | | not specified
1181 4 | 4 | India | IN | 3 | | | not specified
1182 5 | 5 | Japan | JP | 3 | | | Sinzo Abe
1183 6 | 6 | Singapore | SG | 3 | 791 | km | not specified
1186 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int);
1187 COUNTRY_NAME | REGION_ID
1188 --------------+-----------
1193 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY, "COUNTRY_NAME" text, "REGION_ID" int);
1194 id | COUNTRY_NAME | REGION_ID
1195 ----+--------------+-----------
1200 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int);
1201 id | COUNTRY_NAME | REGION_ID
1202 ----+--------------+-----------
1207 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id');
1214 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY);
1221 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH '.');
1222 id | COUNTRY_NAME | REGION_ID | rawdata
1223 ----+--------------+-----------+------------------------------------------------------------------
1224 4 | India | 3 | <ROW id="4"> +
1225 | | | <COUNTRY_ID>IN</COUNTRY_ID> +
1226 | | | <COUNTRY_NAME>India</COUNTRY_NAME> +
1227 | | | <REGION_ID>3</REGION_ID> +
1229 5 | Japan | 3 | <ROW id="5"> +
1230 | | | <COUNTRY_ID>JP</COUNTRY_ID> +
1231 | | | <COUNTRY_NAME>Japan</COUNTRY_NAME> +
1232 | | | <REGION_ID>3</REGION_ID><PREMIER_NAME>Sinzo Abe</PREMIER_NAME>+
1236 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH './*');
1237 id | COUNTRY_NAME | REGION_ID | rawdata
1238 ----+--------------+-----------+-----------------------------------------------------------------------------------------------------------------------------
1239 4 | India | 3 | <COUNTRY_ID>IN</COUNTRY_ID><COUNTRY_NAME>India</COUNTRY_NAME><REGION_ID>3</REGION_ID>
1240 5 | Japan | 3 | <COUNTRY_ID>JP</COUNTRY_ID><COUNTRY_NAME>Japan</COUNTRY_NAME><REGION_ID>3</REGION_ID><PREMIER_NAME>Sinzo Abe</PREMIER_NAME>
1243 SELECT * FROM xmltable('/root' passing '<root><element>a1a<!-- aaaa -->a2a<?aaaaa?> <!--z--> bbbb<x>xxx</x>cccc</element></root>' COLUMNS element text);
1245 ----------------------
1249 SELECT * FROM xmltable('/root' passing '<root><element>a1a<!-- aaaa -->a2a<?aaaaa?> <!--z--> bbbb<x>xxx</x>cccc</element></root>' COLUMNS element text PATH 'element/text()'); -- should fail
1250 ERROR: more than one value returned by column XPath expression
1252 select * from xmltable('d/r' passing '<d><r><c><![CDATA[<hello> &"<>!<a>foo</a>]]></c></r><r><c>2</c></r></d>' columns c text);
1254 -------------------------
1255 <hello> &"<>!<a>foo</a>
1259 -- XML builtin entities
1260 SELECT * FROM xmltable('/x/a' PASSING '<x><a><ent>'</ent></a><a><ent>"</ent></a><a><ent>&</ent></a><a><ent><</ent></a><a><ent>></ent></a></x>' COLUMNS ent text);
1270 SELECT * FROM xmltable('/x/a' PASSING '<x><a><ent>'</ent></a><a><ent>"</ent></a><a><ent>&</ent></a><a><ent><</ent></a><a><ent>></ent></a></x>' COLUMNS ent xml);
1280 EXPLAIN (VERBOSE, COSTS OFF)
1282 FROM (SELECT data FROM xmldata) x,
1283 LATERAL XMLTABLE('/ROWS/ROW'
1285 COLUMNS id int PATH '@id',
1287 country_name text PATH 'COUNTRY_NAME' NOT NULL,
1288 country_id text PATH 'COUNTRY_ID',
1289 region_id int PATH 'REGION_ID',
1290 size float PATH 'SIZE',
1291 unit text PATH 'SIZE/@unit',
1292 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
1294 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1296 Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name
1297 -> Seq Scan on public.xmldata
1298 Output: xmldata.data
1299 -> Table Function Scan on "xmltable"
1300 Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name
1301 Table Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text))
1305 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int) WHERE "COUNTRY_NAME" = 'Japan';
1306 COUNTRY_NAME | REGION_ID
1307 --------------+-----------
1311 EXPLAIN (VERBOSE, COSTS OFF)
1312 SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int) WHERE "COUNTRY_NAME" = 'Japan';
1314 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1316 Output: "xmltable"."COUNTRY_NAME", "xmltable"."REGION_ID"
1317 -> Seq Scan on public.xmldata
1318 Output: xmldata.data
1319 -> Table Function Scan on "xmltable"
1320 Output: "xmltable"."COUNTRY_NAME", "xmltable"."REGION_ID"
1321 Table Function Call: XMLTABLE(('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]'::text) PASSING (xmldata.data) COLUMNS "COUNTRY_NAME" text, "REGION_ID" integer)
1322 Filter: ("xmltable"."COUNTRY_NAME" = 'Japan'::text)
1325 -- should to work with more data
1326 INSERT INTO xmldata VALUES('<ROWS>
1328 <COUNTRY_ID>CZ</COUNTRY_ID>
1329 <COUNTRY_NAME>Czech Republic</COUNTRY_NAME>
1330 <REGION_ID>2</REGION_ID><PREMIER_NAME>Milos Zeman</PREMIER_NAME>
1333 <COUNTRY_ID>DE</COUNTRY_ID>
1334 <COUNTRY_NAME>Germany</COUNTRY_NAME>
1335 <REGION_ID>2</REGION_ID>
1338 <COUNTRY_ID>FR</COUNTRY_ID>
1339 <COUNTRY_NAME>France</COUNTRY_NAME>
1340 <REGION_ID>2</REGION_ID>
1343 INSERT INTO xmldata VALUES('<ROWS>
1345 <COUNTRY_ID>EG</COUNTRY_ID>
1346 <COUNTRY_NAME>Egypt</COUNTRY_NAME>
1347 <REGION_ID>1</REGION_ID>
1350 <COUNTRY_ID>SD</COUNTRY_ID>
1351 <COUNTRY_NAME>Sudan</COUNTRY_NAME>
1352 <REGION_ID>1</REGION_ID>
1356 FROM (SELECT data FROM xmldata) x,
1357 LATERAL XMLTABLE('/ROWS/ROW'
1359 COLUMNS id int PATH '@id',
1361 country_name text PATH 'COUNTRY_NAME' NOT NULL,
1362 country_id text PATH 'COUNTRY_ID',
1363 region_id int PATH 'REGION_ID',
1364 size float PATH 'SIZE',
1365 unit text PATH 'SIZE/@unit',
1366 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
1367 id | _id | country_name | country_id | region_id | size | unit | premier_name
1368 ----+-----+----------------+------------+-----------+------+------+---------------
1369 1 | 1 | Australia | AU | 3 | | | not specified
1370 2 | 2 | China | CN | 3 | | | not specified
1371 3 | 3 | HongKong | HK | 3 | | | not specified
1372 4 | 4 | India | IN | 3 | | | not specified
1373 5 | 5 | Japan | JP | 3 | | | Sinzo Abe
1374 6 | 6 | Singapore | SG | 3 | 791 | km | not specified
1375 10 | 1 | Czech Republic | CZ | 2 | | | Milos Zeman
1376 11 | 2 | Germany | DE | 2 | | | not specified
1377 12 | 3 | France | FR | 2 | | | not specified
1378 20 | 1 | Egypt | EG | 1 | | | not specified
1379 21 | 2 | Sudan | SD | 1 | | | not specified
1383 FROM (SELECT data FROM xmldata) x,
1384 LATERAL XMLTABLE('/ROWS/ROW'
1386 COLUMNS id int PATH '@id',
1388 country_name text PATH 'COUNTRY_NAME' NOT NULL,
1389 country_id text PATH 'COUNTRY_ID',
1390 region_id int PATH 'REGION_ID',
1391 size float PATH 'SIZE',
1392 unit text PATH 'SIZE/@unit',
1393 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified')
1394 WHERE region_id = 2;
1395 id | _id | country_name | country_id | region_id | size | unit | premier_name
1396 ----+-----+----------------+------------+-----------+------+------+---------------
1397 10 | 1 | Czech Republic | CZ | 2 | | | Milos Zeman
1398 11 | 2 | Germany | DE | 2 | | | not specified
1399 12 | 3 | France | FR | 2 | | | not specified
1402 EXPLAIN (VERBOSE, COSTS OFF)
1404 FROM (SELECT data FROM xmldata) x,
1405 LATERAL XMLTABLE('/ROWS/ROW'
1407 COLUMNS id int PATH '@id',
1409 country_name text PATH 'COUNTRY_NAME' NOT NULL,
1410 country_id text PATH 'COUNTRY_ID',
1411 region_id int PATH 'REGION_ID',
1412 size float PATH 'SIZE',
1413 unit text PATH 'SIZE/@unit',
1414 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified')
1415 WHERE region_id = 2;
1417 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1419 Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name
1420 -> Seq Scan on public.xmldata
1421 Output: xmldata.data
1422 -> Table Function Scan on "xmltable"
1423 Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name
1424 Table Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text))
1425 Filter: ("xmltable".region_id = 2)
1428 -- should fail, NULL value
1430 FROM (SELECT data FROM xmldata) x,
1431 LATERAL XMLTABLE('/ROWS/ROW'
1433 COLUMNS id int PATH '@id',
1435 country_name text PATH 'COUNTRY_NAME' NOT NULL,
1436 country_id text PATH 'COUNTRY_ID',
1437 region_id int PATH 'REGION_ID',
1438 size float PATH 'SIZE' NOT NULL,
1439 unit text PATH 'SIZE/@unit',
1440 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
1441 ERROR: null is not allowed in column "size"
1442 -- if all is ok, then result is empty
1443 -- one line xml test
1445 x AS (SELECT proname, proowner, procost::numeric, pronargs,
1446 array_to_string(proargnames,',') as proargnames,
1447 case when proargtypes <> '' then array_to_string(proargtypes::oid[],',') end as proargtypes
1448 FROM pg_proc WHERE proname = 'f_leak'),
1449 y AS (SELECT xmlelement(name proc,
1450 xmlforest(proname, proowner,
1452 proargnames, proargtypes)) as proc
1454 z AS (SELECT xmltable.*
1456 LATERAL xmltable('/proc' PASSING proc
1457 COLUMNS proname name,
1464 EXCEPT SELECT * FROM x;
1465 proname | proowner | procost | pronargs | proargnames | proargtypes
1466 ---------+----------+---------+----------+-------------+-------------
1469 -- multi line xml test, result should be empty too
1471 x AS (SELECT proname, proowner, procost::numeric, pronargs,
1472 array_to_string(proargnames,',') as proargnames,
1473 case when proargtypes <> '' then array_to_string(proargtypes::oid[],',') end as proargtypes
1475 y AS (SELECT xmlelement(name data,
1476 xmlagg(xmlelement(name proc,
1477 xmlforest(proname, proowner, procost,
1478 pronargs, proargnames, proargtypes)))) as doc
1480 z AS (SELECT xmltable.*
1482 LATERAL xmltable('/data/proc' PASSING doc
1483 COLUMNS proname name,
1490 EXCEPT SELECT * FROM x;
1491 proname | proowner | procost | pronargs | proargnames | proargtypes
1492 ---------+----------+---------+----------+-------------+-------------
1495 CREATE TABLE xmltest2(x xml, _path text);
1496 INSERT INTO xmltest2 VALUES('<d><r><ac>1</ac></r></d>', 'A');
1497 INSERT INTO xmltest2 VALUES('<d><r><bc>2</bc></r></d>', 'B');
1498 INSERT INTO xmltest2 VALUES('<d><r><cc>3</cc></r></d>', 'C');
1499 INSERT INTO xmltest2 VALUES('<d><r><dc>2</dc></r></d>', 'D');
1500 SELECT xmltable.* FROM xmltest2, LATERAL xmltable('/d/r' PASSING x COLUMNS a int PATH '' || lower(_path) || 'c');
1509 SELECT xmltable.* FROM xmltest2, LATERAL xmltable(('/d/r/' || lower(_path) || 'c') PASSING x COLUMNS a int PATH '.');
1518 SELECT xmltable.* FROM xmltest2, LATERAL xmltable(('/d/r/' || lower(_path) || 'c') PASSING x COLUMNS a int PATH 'x' DEFAULT ascii(_path) - 54);
1527 -- XPath result can be boolean or number too
1528 SELECT * FROM XMLTABLE('*' PASSING '<a>a</a>' COLUMNS a xml PATH '.', b text PATH '.', c text PATH '"hi"', d boolean PATH '. = "a"', e integer PATH 'string-length(.)');
1530 ----------+---+----+---+---
1531 <a>a</a> | a | hi | t | 1
1535 SELECT * FROM XMLTABLE('*' PASSING '<e>pre<!--c1--><?pi arg?><![CDATA[&ent1]]><n2>&deep</n2>post</e>' COLUMNS x xml PATH 'node()', y xml PATH '/');
1536 -[ RECORD 1 ]-----------------------------------------------------------
1537 x | pre<!--c1--><?pi arg?><![CDATA[&ent1]]><n2>&deep</n2>post
1538 y | <e>pre<!--c1--><?pi arg?><![CDATA[&ent1]]><n2>&deep</n2>post</e>+
1542 SELECT * FROM XMLTABLE('.' PASSING XMLELEMENT(NAME a) columns a varchar(20) PATH '"<foo/>"', b xml PATH '"<foo/>"');
1544 --------+--------------
1545 <foo/> | <foo/>