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: Entity: line 1: parser error : Couldn't find end of Start Tag wrong line 1
14 SELECT * FROM xmltest;
16 ----+--------------------
17 1 | <value>one</value>
18 2 | <value>two</value>
21 SELECT xmlcomment('test');
27 SELECT xmlcomment('-test');
33 SELECT xmlcomment('test-');
34 ERROR: invalid XML comment
35 SELECT xmlcomment('--test');
36 ERROR: invalid XML comment
37 SELECT xmlcomment('te st');
43 SELECT xmlconcat(xmlcomment('hello'),
44 xmlelement(NAME qux, 'foo'),
47 ----------------------------------------
48 <!--hello--><qux>foo</qux><!--world-->
51 SELECT xmlconcat('hello', 'you');
57 SELECT xmlconcat(1, 2);
58 ERROR: argument of XMLCONCAT must be type xml, not type integer
59 LINE 1: SELECT xmlconcat(1, 2);
61 SELECT xmlconcat('bad', '<syntax');
62 ERROR: invalid XML content
63 LINE 1: SELECT xmlconcat('bad', '<syntax');
65 DETAIL: Entity: line 1: parser error : Couldn't find end of Start Tag syntax line 1
68 SELECT xmlconcat('<foo/>', NULL, '<?xml version="1.1" standalone="no"?><bar/>');
74 SELECT xmlconcat('<?xml version="1.1"?><foo/>', NULL, '<?xml version="1.1" standalone="no"?><bar/>');
76 -----------------------------------
77 <?xml version="1.1"?><foo/><bar/>
80 SELECT xmlconcat(NULL);
86 SELECT xmlconcat(NULL, NULL);
92 SELECT xmlelement(name element,
93 xmlattributes (1 as one, 'deuce' as two),
96 ------------------------------------------------
97 <element one="1" two="deuce">content</element>
100 SELECT xmlelement(name element,
101 xmlattributes ('unnamed and wrong'));
102 ERROR: unnamed XML attribute value must be a column reference
103 LINE 2: xmlattributes ('unnamed and wrong'));
105 SELECT xmlelement(name element, xmlelement(name nested, 'stuff'));
107 -------------------------------------------
108 <element><nested>stuff</nested></element>
111 SELECT xmlelement(name employee, xmlforest(name, age, salary as pay)) FROM emp;
113 ----------------------------------------------------------------------
114 <employee><name>sharon</name><age>25</age><pay>1000</pay></employee>
115 <employee><name>sam</name><age>30</age><pay>2000</pay></employee>
116 <employee><name>bill</name><age>20</age><pay>1000</pay></employee>
117 <employee><name>jeff</name><age>23</age><pay>600</pay></employee>
118 <employee><name>cim</name><age>30</age><pay>400</pay></employee>
119 <employee><name>linda</name><age>19</age><pay>100</pay></employee>
122 SELECT xmlelement(name duplicate, xmlattributes(1 as a, 2 as b, 3 as a));
123 ERROR: XML attribute name "a" appears more than once
124 LINE 1: ...ment(name duplicate, xmlattributes(1 as a, 2 as b, 3 as a));
126 SELECT xmlelement(name num, 37);
132 SELECT xmlelement(name foo, text 'bar');
138 SELECT xmlelement(name foo, xml 'bar');
144 SELECT xmlelement(name foo, text 'b<a/>r');
146 -------------------------
147 <foo>b<a/>r</foo>
150 SELECT xmlelement(name foo, xml 'b<a/>r');
156 SELECT xmlelement(name foo, array[1, 2, 3]);
158 -------------------------------------------------------------------------
159 <foo><element>1</element><element>2</element><element>3</element></foo>
162 SET xmlbinary TO base64;
163 SELECT xmlelement(name foo, bytea 'bar');
169 SET xmlbinary TO hex;
170 SELECT xmlelement(name foo, bytea 'bar');
176 SELECT xmlelement(name foo, xmlattributes(true as bar));
182 SELECT xmlelement(name foo, xmlattributes('2009-04-09 00:24:37'::timestamp as bar));
184 ----------------------------------
185 <foo bar="2009-04-09T00:24:37"/>
188 SELECT xmlelement(name foo, xmlattributes('infinity'::timestamp as bar));
189 ERROR: timestamp out of range
190 DETAIL: XML does not support infinite timestamp values.
191 SELECT xmlelement(name foo, xmlattributes('<>&"''' as funny, xml 'b<a/>r' as funnier));
193 ------------------------------------------------------------
194 <foo funny="<>&"'" funnier="b<a/>r"/>
197 SELECT xmlparse(content 'abc');
203 SELECT xmlparse(content '<abc>x</abc>');
209 SELECT xmlparse(document 'abc');
210 ERROR: invalid XML document
211 DETAIL: Entity: line 1: parser error : Start tag expected, '<' not found
214 SELECT xmlparse(document '<abc>x</abc>');
220 SELECT xmlpi(name foo);
226 SELECT xmlpi(name xml);
227 ERROR: invalid XML processing instruction
228 DETAIL: XML processing instruction target name cannot be "xml".
229 SELECT xmlpi(name xmlstuff);
235 SELECT xmlpi(name foo, 'bar');
241 SELECT xmlpi(name foo, 'in?>valid');
242 ERROR: invalid XML processing instruction
243 DETAIL: XML processing instruction cannot contain "?>".
244 SELECT xmlpi(name foo, null);
250 SELECT xmlpi(name xml, null);
251 ERROR: invalid XML processing instruction
252 DETAIL: XML processing instruction target name cannot be "xml".
253 SELECT xmlpi(name xmlstuff, null);
259 SELECT xmlpi(name "xml-stylesheet", 'href="mystyle.css" type="text/css"');
261 -------------------------------------------------------
262 <?xml-stylesheet href="mystyle.css" type="text/css"?>
265 SELECT xmlpi(name foo, ' bar');
271 SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
277 SELECT xmlroot(xml '<foo/>', version '2.0');
279 -----------------------------
280 <?xml version="2.0"?><foo/>
283 SELECT xmlroot(xml '<foo/>', version no value, standalone yes);
285 ----------------------------------------------
286 <?xml version="1.0" standalone="yes"?><foo/>
289 SELECT xmlroot(xml '<?xml version="1.1"?><foo/>', version no value, standalone yes);
291 ----------------------------------------------
292 <?xml version="1.0" standalone="yes"?><foo/>
295 SELECT xmlroot(xmlroot(xml '<foo/>', version '1.0'), version '1.1', standalone no);
297 ---------------------------------------------
298 <?xml version="1.1" standalone="no"?><foo/>
301 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value, standalone no);
303 ---------------------------------------------
304 <?xml version="1.0" standalone="no"?><foo/>
307 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value, standalone no value);
313 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value);
315 ----------------------------------------------
316 <?xml version="1.0" standalone="yes"?><foo/>
335 ------------------------------------------------------------------------------------------
336 <?xml version="1.0" standalone="yes"?><gazonk name="val" num="2"><qux>foo</qux></gazonk>
339 SELECT xmlserialize(content data as character varying(20)) FROM xmltest;
346 SELECT xmlserialize(content 'good' as char(10));
352 SELECT xmlserialize(document 'bad' as text);
353 ERROR: not an XML document
354 SELECT xml '<foo>bar</foo>' IS DOCUMENT;
360 SELECT xml '<foo>bar</foo><bar>foo</bar>' IS DOCUMENT;
366 SELECT xml '<abc/>' IS NOT DOCUMENT;
372 SELECT xml 'abc' IS NOT DOCUMENT;
378 SELECT '<>' IS NOT DOCUMENT;
379 ERROR: invalid XML content
380 LINE 1: SELECT '<>' IS NOT DOCUMENT;
382 DETAIL: Entity: line 1: parser error : StartTag: invalid element name
385 SELECT xmlagg(data) FROM xmltest;
387 --------------------------------------
388 <value>one</value><value>two</value>
391 SELECT xmlagg(data) FROM xmltest WHERE id > 10;
397 SELECT xmlelement(name employees, xmlagg(xmlelement(name name, name))) FROM emp;
399 --------------------------------------------------------------------------------------------------------------------------------
400 <employees><name>sharon</name><name>sam</name><name>bill</name><name>jeff</name><name>cim</name><name>linda</name></employees>
403 -- Check mapping SQL identifier to XML name
404 SELECT xmlpi(name ":::_xml_abc135.%-&_");
406 -------------------------------------------------
407 <?_x003A_::_x005F_xml_abc135._x0025_-_x0026__?>
410 SELECT xmlpi(name "123");
416 PREPARE foo (xml) AS SELECT xmlconcat('<foo/>', $1);
417 SET XML OPTION DOCUMENT;
418 EXECUTE foo ('<bar/>');
425 ERROR: invalid XML document
426 LINE 1: EXECUTE foo ('bad');
428 DETAIL: Entity: line 1: parser error : Start tag expected, '<' not found
431 SET XML OPTION CONTENT;
432 EXECUTE foo ('<bar/>');
438 EXECUTE foo ('good');
444 -- Test backwards parsing
445 CREATE VIEW xmlview1 AS SELECT xmlcomment('test');
446 CREATE VIEW xmlview2 AS SELECT xmlconcat('hello', 'you');
447 CREATE VIEW xmlview3 AS SELECT xmlelement(name element, xmlattributes (1 as ":one:", 'deuce' as two), 'content&');
448 CREATE VIEW xmlview4 AS SELECT xmlelement(name employee, xmlforest(name, age, salary as pay)) FROM emp;
449 CREATE VIEW xmlview5 AS SELECT xmlparse(content '<abc>x</abc>');
450 CREATE VIEW xmlview6 AS SELECT xmlpi(name foo, 'bar');
451 CREATE VIEW xmlview7 AS SELECT xmlroot(xml '<foo/>', version no value, standalone yes);
452 CREATE VIEW xmlview8 AS SELECT xmlserialize(content 'good' as char(10));
453 CREATE VIEW xmlview9 AS SELECT xmlserialize(content 'good' as text);
454 SELECT table_name, view_definition FROM information_schema.views
455 WHERE table_name LIKE 'xmlview%' ORDER BY 1;
456 table_name | view_definition
457 ------------+----------------------------------------------------------------------------------------------------------------------------
458 xmlview1 | SELECT xmlcomment('test'::text) AS xmlcomment;
459 xmlview2 | SELECT XMLCONCAT('hello'::xml, 'you'::xml) AS "xmlconcat";
460 xmlview3 | SELECT XMLELEMENT(NAME element, XMLATTRIBUTES(1 AS ":one:", 'deuce' AS two), 'content&') AS "xmlelement";
461 xmlview4 | SELECT XMLELEMENT(NAME employee, XMLFOREST(emp.name AS name, emp.age AS age, emp.salary AS pay)) AS "xmlelement" FROM emp;
462 xmlview5 | SELECT XMLPARSE(CONTENT '<abc>x</abc>'::text STRIP WHITESPACE) AS "xmlparse";
463 xmlview6 | SELECT XMLPI(NAME foo, 'bar'::text) AS "xmlpi";
464 xmlview7 | SELECT XMLROOT('<foo/>'::xml, VERSION NO VALUE, STANDALONE YES) AS "xmlroot";
465 xmlview8 | SELECT (XMLSERIALIZE(CONTENT 'good'::xml AS character(10)))::character(10) AS "xmlserialize";
466 xmlview9 | SELECT XMLSERIALIZE(CONTENT 'good'::xml AS text) AS "xmlserialize";
469 -- Text XPath expressions evaluation
470 SELECT xpath('/value', data) FROM xmltest;
472 ----------------------
477 SELECT xpath(NULL, NULL) IS NULL FROM xmltest;
484 SELECT xpath('', '<!-- error -->');
485 ERROR: empty XPath expression
486 CONTEXT: SQL function "xpath" statement 1
487 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>');
493 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']]);
499 SELECT xpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
501 -------------------------
502 {<b>two</b>,<b>etc</b>}