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 DETAIL: Entity: line 1: parser error : 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 SELECT xmlconcat('bad', '<syntax');
58 ERROR: invalid XML content
59 DETAIL: Entity: line 1: parser error : Couldn't find end of Start Tag syntax line 1
62 SELECT xmlconcat('<foo/>', NULL, '<?xml version="1.1" standalone="no"?><bar/>');
68 SELECT xmlconcat('<?xml version="1.1"?><foo/>', NULL, '<?xml version="1.1" standalone="no"?><bar/>');
70 -----------------------------------
71 <?xml version="1.1"?><foo/><bar/>
74 SELECT xmlelement(name element,
75 xmlattributes (1 as one, 'deuce' as two),
78 ------------------------------------------------
79 <element one="1" two="deuce">content</element>
82 SELECT xmlelement(name element,
83 xmlattributes ('unnamed and wrong'));
84 ERROR: unnamed XML attribute value must be a column reference
85 SELECT xmlelement(name element, xmlelement(name nested, 'stuff'));
87 -------------------------------------------
88 <element><nested>stuff</nested></element>
91 SELECT xmlelement(name employee, xmlforest(name, age, salary as pay)) FROM emp;
93 ----------------------------------------------------------------------
94 <employee><name>sharon</name><age>25</age><pay>1000</pay></employee>
95 <employee><name>sam</name><age>30</age><pay>2000</pay></employee>
96 <employee><name>bill</name><age>20</age><pay>1000</pay></employee>
97 <employee><name>jeff</name><age>23</age><pay>600</pay></employee>
98 <employee><name>cim</name><age>30</age><pay>400</pay></employee>
99 <employee><name>linda</name><age>19</age><pay>100</pay></employee>
102 SELECT xmlelement(name duplicate, xmlattributes(1 as a, 2 as b, 3 as a));
103 ERROR: XML attribute name "a" appears more than once
104 SELECT xmlelement(name num, 37);
110 SELECT xmlelement(name foo, text 'bar');
116 SELECT xmlelement(name foo, xml 'bar');
122 SELECT xmlelement(name foo, text 'b<a/>r');
124 -------------------------
125 <foo>b<a/>r</foo>
128 SELECT xmlelement(name foo, xml 'b<a/>r');
134 SELECT xmlelement(name foo, array[1, 2, 3]);
136 -------------------------------------------------------------------------
137 <foo><element>1</element><element>2</element><element>3</element></foo>
140 SET xmlbinary TO base64;
141 SELECT xmlelement(name foo, bytea 'bar');
147 SET xmlbinary TO hex;
148 SELECT xmlelement(name foo, bytea 'bar');
154 SELECT xmlparse(content 'abc');
160 SELECT xmlparse(content '<abc>x</abc>');
166 SELECT xmlparse(document 'abc');
167 ERROR: invalid XML document
168 DETAIL: Entity: line 1: parser error : Start tag expected, '<' not found
171 SELECT xmlparse(document '<abc>x</abc>');
177 SELECT xmlpi(name foo);
183 SELECT xmlpi(name xmlstuff);
184 ERROR: invalid XML processing instruction
185 DETAIL: XML processing instruction target name cannot start with "xml".
186 SELECT xmlpi(name foo, 'bar');
192 SELECT xmlpi(name foo, 'in?>valid');
193 ERROR: invalid XML processing instruction
194 DETAIL: XML processing instruction cannot contain "?>".
195 SELECT xmlpi(name foo, null);
201 SELECT xmlpi(name xmlstuff, null);
202 ERROR: invalid XML processing instruction
203 DETAIL: XML processing instruction target name cannot start with "xml".
204 SELECT xmlpi(name foo, ' bar');
210 SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
216 SELECT xmlroot(xml '<foo/>', version '2.0');
218 -----------------------------
219 <?xml version="2.0"?><foo/>
222 SELECT xmlroot(xml '<foo/>', version no value, standalone yes);
224 ----------------------------------------------
225 <?xml version="1.0" standalone="yes"?><foo/>
228 SELECT xmlroot(xml '<?xml version="1.1"?><foo/>', version no value, standalone yes);
230 ----------------------------------------------
231 <?xml version="1.0" standalone="yes"?><foo/>
234 SELECT xmlroot(xmlroot(xml '<foo/>', version '1.0'), version '1.1', standalone no);
236 ---------------------------------------------
237 <?xml version="1.1" standalone="no"?><foo/>
240 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value, standalone no);
242 ---------------------------------------------
243 <?xml version="1.0" standalone="no"?><foo/>
246 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value, standalone no value);
252 SELECT xmlroot('<?xml version="1.1" standalone="yes"?><foo/>', version no value);
254 ----------------------------------------------
255 <?xml version="1.0" standalone="yes"?><foo/>
274 ------------------------------------------------------------------------------------------
275 <?xml version="1.0" standalone="yes"?><gazonk name="val" num="2"><qux>foo</qux></gazonk>
278 SELECT xmlserialize(content data as character varying(20)) FROM xmltest;
285 SELECT xmlserialize(content 'good' as char(10));
291 SELECT xmlserialize(document 'bad' as text);
292 ERROR: not an XML document
293 SELECT xml '<foo>bar</foo>' IS DOCUMENT;
299 SELECT xml '<foo>bar</foo><bar>foo</bar>' IS DOCUMENT;
305 SELECT xml '<abc/>' IS NOT DOCUMENT;
311 SELECT xml 'abc' IS NOT DOCUMENT;
317 SELECT '<>' IS NOT DOCUMENT;
318 ERROR: invalid XML content
319 DETAIL: Entity: line 1: parser error : StartTag: invalid element name
322 SELECT xmlagg(data) FROM xmltest;
324 --------------------------------------
325 <value>one</value><value>two</value>
328 SELECT xmlagg(data) FROM xmltest WHERE id > 10;
334 SELECT xmlelement(name employees, xmlagg(xmlelement(name name, name))) FROM emp;
336 --------------------------------------------------------------------------------------------------------------------------------
337 <employees><name>sharon</name><name>sam</name><name>bill</name><name>jeff</name><name>cim</name><name>linda</name></employees>
340 -- Check mapping SQL identifier to XML name
341 SELECT xmlpi(name ":::_xml_abc135.%-&_");
343 -------------------------------------------------
344 <?_x003A_::_x005F_xml_abc135._x0025_-_x0026__?>
347 SELECT xmlpi(name "123");
353 PREPARE foo (xml) AS SELECT xmlconcat('<foo/>', $1);
354 SET XML OPTION DOCUMENT;
355 EXECUTE foo ('<bar/>');
362 ERROR: invalid XML document
363 DETAIL: Entity: line 1: parser error : Start tag expected, '<' not found
366 SET XML OPTION CONTENT;
367 EXECUTE foo ('<bar/>');
373 EXECUTE foo ('good');
379 -- Test backwards parsing
380 CREATE VIEW xmlview1 AS SELECT xmlcomment('test');
381 CREATE VIEW xmlview2 AS SELECT xmlconcat('hello', 'you');
382 CREATE VIEW xmlview3 AS SELECT xmlelement(name element, xmlattributes (1 as ":one:", 'deuce' as two), 'content&');
383 CREATE VIEW xmlview4 AS SELECT xmlelement(name employee, xmlforest(name, age, salary as pay)) FROM emp;
384 CREATE VIEW xmlview5 AS SELECT xmlparse(content '<abc>x</abc>');
385 CREATE VIEW xmlview6 AS SELECT xmlpi(name foo, 'bar');
386 CREATE VIEW xmlview7 AS SELECT xmlroot(xml '<foo/>', version no value, standalone yes);
387 CREATE VIEW xmlview8 AS SELECT xmlserialize(content 'good' as char(10));
388 CREATE VIEW xmlview9 AS SELECT xmlserialize(content 'good' as text);
389 SELECT table_name, view_definition FROM information_schema.views
390 WHERE table_name LIKE 'xmlview%' ORDER BY 1;
391 table_name | view_definition
392 ------------+----------------------------------------------------------------------------------------------------------------------------
393 xmlview1 | SELECT xmlcomment('test'::text) AS xmlcomment;
394 xmlview2 | SELECT XMLCONCAT('hello'::xml, 'you'::xml) AS "xmlconcat";
395 xmlview3 | SELECT XMLELEMENT(NAME element, XMLATTRIBUTES(1 AS ":one:", 'deuce' AS two), 'content&') AS "xmlelement";
396 xmlview4 | SELECT XMLELEMENT(NAME employee, XMLFOREST(emp.name AS name, emp.age AS age, emp.salary AS pay)) AS "xmlelement" FROM emp;
397 xmlview5 | SELECT XMLPARSE(CONTENT '<abc>x</abc>'::text STRIP WHITESPACE) AS "xmlparse";
398 xmlview6 | SELECT XMLPI(NAME foo, 'bar'::text) AS "xmlpi";
399 xmlview7 | SELECT XMLROOT('<foo/>'::xml, VERSION NO VALUE, STANDALONE YES) AS "xmlroot";
400 xmlview8 | SELECT (XMLSERIALIZE(CONTENT 'good'::xml AS character(10)))::character(10) AS "xmlserialize";
401 xmlview9 | SELECT XMLSERIALIZE(CONTENT 'good'::xml AS text) AS "xmlserialize";
404 -- Text XPath expressions evaluation
405 SELECT xpath('/value', data) FROM xmltest;
407 ----------------------
412 SELECT xpath(NULL, NULL) IS NULL FROM xmltest;
419 SELECT xpath('', '<!-- error -->');
420 ERROR: empty XPath expression
421 CONTEXT: SQL function "xpath" statement 1
422 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>');
428 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']]);
434 SELECT xpath('//b', '<a>one <b>two</b> three <b>etc</b></a>');
436 -------------------------
437 {<b>two</b>,<b>etc</b>}