XML operation function code in PostgreSQL _postgresql

Source: Internet
Author: User
Tags postgresql xpath xpath functions

XML Content Generation section
SQL data to generate XML functions.
1. Xmlcomment: Generate annotation functions.
Xmlcomment (text)
Cases:

SELECT xmlcomment (' hello ');
Xmlcomment
--------------
<!--hello-->

2. xmlconcat:xml Connection function
Xmlconcat (XML [, ...])
Cases:
SELECT xmlconcat (' <abc/> ', ' <bar>foo</bar> ');

Xmlconcat
----------------------
<abc/><bar>foo</bar>

If multiple versions are declared in the connected XML data, the attached XML has only one version declaration.
Cases:
SELECT xmlconcat (' <?xml version= ' 1.1 '?><foo/> ', ' <?xml version= ' 1.1 ' standalone= ' no '? ><bar/ > ');

Xmlconcat
-----------------------------------
<?xml version= "1.1"?><foo/><bar/>

3. XmlElement: Generating XML element Functions
XmlElement (name name [, XmlAttributes (value [as AttName] [, ...])] [, Content, ...])
Cases:
SELECT XmlElement (name foo);
XmlElement
------------
<foo/>

SELECT XmlElement (name foo, xmlattributes (' xyz ' as bar);
XmlElement
------------------
<foo bar= "xyz"/>

SELECT XmlElement (name foo, xmlattributes (current_date as bar), ' cont ', ' ent ');
XmlElement
-------------------------------------
<foo bar= "2007-01-26" >content</foo>

If there are illegal characters, the very characters are represented by a number of 16 digits.
Example: SELECT XmlElement (name "Foo$bar", XmlAttributes (' xyz ' as "a&b"));
XmlElement
----------------------------------
<foo_x0024_bar a_x0026_b= "xyz"/>

4. Xmlforest: Generate XML Forest function
Xmlforest (content [as name] [, ...])
Cases:
SELECT xmlforest (' abc ' as FOO, 123 as bar);
Xmlforest
------------------------------
<foo>abc</foo><bar>123</bar>

SELECT xmlforest (table_name, COLUMN_NAME)
From Information_schema.columns
WHERE table_schema = ' pg_catalog ';
Xmlforest
-------------------------------------------------------------------------------------------
<table_name>pg_authid</table_name><column_name>rolname</column_name>
<table_name>pg_authid</table_name><column_name>rolsuper</column_name>
...
5. XMLPI: Generates XML processing command functions.
XMLPI (name target [, content])
Cases:
SELECT Xmlpi (name PHP, ' echo ' Hello World; ');
Xmlpi
-----------------------------
<?php echo "Hello World";? >

6. XmlRoot: Modifying the root node property function of an XML value
XmlRoot (XML, version text |no value [, standalone Yes|no|no value])
Example: SELECT xmlroot (Xmlparse (document ' <?xml version= ' 1.1 '?><content>abc</content> '),
Version ' 1.0 ', standalone YES);
XmlRoot
----------------------------------------
<?xml version= "1.0" standalone= "yes"?>
<content>abc</content>

7. Xmlagg:xmlagg is an intensive function
Xmlagg (XML)
Cases:
CREATE TABLE Test (y int, x XML);
INSERT into Test VALUES (1, ' <foo>abc</foo> ');
INSERT into Test VALUES (2, ' <bar/> ');
SELECT Xmlagg (x) from test;
Xmlagg
----------------------
<foo>abc</foo><bar/>

You can change the connection order in the following types of methods.
Select Xmlagg (x) from (SELECT * from Test-y DESC) as tab;
Xmlagg
----------------------
<bar/><foo>abc</foo>

Processing XML
In order to process XML data, XPath functions are provided in POSTGRESL.
XPath (XPath, XML [, Nsarray])

Cases:
SELECT XPath ('/my:a/text () ', ' <my:a xmlns:my= ' http://example.com ' >test</my:a> ',
Array[array[' my ', ' http://example.com ']);
Xpath
--------
{Test}
(1 row)

Maps of XML and table
The following function can export XML.
Table_to_xml (TBL Regclass, nulls Boolean, Tableforest Boolean, targetns text)
Query_to_xml (query text, nulls Boolean, Tableforest Boolean, targetns text)
Cursor_to_xml (cursor refcursor, Count int, nulls Boolean,
Tableforest Boolean, Targetns text)
The return values of these functions are all XML types.

The following functions are also available. Details can refer to the user manual.
Table_to_xmlschema (TBL Regclass, nulls Boolean, Tableforest Boolean, targetns text)
Query_to_xmlschema (query text, nulls Boolean, Tableforest Boolean, targetns text)
Cursor_to_xmlschema (Cursor Refcursor, nulls Boolean, Tableforest Boolean, targetns text)
Table_to_xml_and_xmlschema (TBL Regclass, nulls Boolean, Tableforest Boolean, targetns text)
Query_to_xml_and_xmlschema (query text, nulls Boolean, Tableforest Boolean, targetns text)
Schema_to_xml (schema name, nulls Boolean, Tableforest Boolean, targetns text)
Schema_to_xmlschema (schema name, nulls Boolean, Tableforest Boolean, targetns text)
Schema_to_xml_and_xmlschema (schema name, nulls Boolean, Tableforest Boolean, targetns text)
Database_to_xml (Nulls Boolean, Tableforest Boolean, targetns text)
Database_to_xmlschema (Nulls Boolean, Tableforest Boolean, targetns text)
Database_to_xml_and_xmlschema (Nulls Boolean, Tableforest Boolean, targetns text)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.