PostgreSQL中的XML操作函数代码

所属分类: 数据库 / PostgreSQL 阅读数: 109
收藏 0 赞 0 分享

XML内容生成部分
SQL数据生成XML的函数。
1. xmlcomment:生成注释函数。
xmlcomment(text )
例:

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

2. xmlconcat:XML连接函数
xmlconcat(xml [, ...])
例:
SELECT xmlconcat('<abc/>', '<bar>foo</bar>');

xmlconcat
----------------------
<abc/><bar>foo</bar>

连接的XML数据中如果有多个版本声明的话,连接后的XML只有一个版本声明。
例:
SELECT xmlconcat('<?xml version="1.1"?><foo/>', '<?xml version="1.1" standalone="no"?><bar/>');

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

3. xmlelement:生成XML元素函数
xmlelement(name name [, xmlattributes( value [AS attname ] [, ... ])] [ , content, ... ])
例:
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>

如果有非法字符的话,非常字符会用 16进制的数字表示出来。
例子:SELECT xmlelement(name "foo$bar", xmlattributes('xyz' as "a&b"));
xmlelement
----------------------------------
<foo_x0024_bar a_x0026_b="xyz"/>

4. xmlforest:生成XML FOREST函数
xmlforest(content [AS name ] [, ...])
例:
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:生成XML处理命令函数。
xmlpi(name target [, content ])
例:
SELECT xmlpi(name php, 'echo "hello world";');
xmlpi
-----------------------------
<?php echo "hello world";?>

6. xmlroot:修改XML值的根节点属性函数
xmlroot(xml , version text |no value [, standalone yes|no|no value])
例子: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是集约函数
xmlagg(xml )
例:
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/>

可以用下面类型的方法改变连接顺序。
SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
xmlagg
----------------------
<bar/><foo>abc</foo>

Processing XML
为了处理XML数据,PostgreSL中提供了xpath函数。
xpath(xpath , xml [, nsarray ])

例:
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)

XML和table的映射
以下函数可以导出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)
这些函数的返回值都是XML类型。

还有以下函数。具体内容可以参看用户手册。
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)

更多精彩内容其他人还在看

SQLite教程(七):数据类型详解

这篇文章主要介绍了SQLite教程(七):数据类型详解,本文讲解了存储种类和数据类型、类型亲缘性、比较表达式、操作符等内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Postgresql ALTER语句常用操作小结

这篇文章主要介绍了Postgresql ALTER语句常用操作小结,本文讲解了增加一列、删除一列、更改列的数据类型、表的重命名、更改列的名字、字段的not null设置等常用操作的代码示例,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读PostgreSQL中的序列及其相关函数的用法

这篇文章主要介绍了PostgreSQL中的序列及其相关函数的用法,包括序列的更新和删除等重要知识,需要的朋友可以参考下
收藏 0 赞 0 分享

PostgreSQL中常用的时间日期脚本使用教程

PostgreSQL是一款简介而又性能强大的数据库应用程序,其在日期时间数据方面所支持的功能也都非常给力,下面就来看一下PostgreSQL中常用的日期时间脚本使用教程.
收藏 0 赞 0 分享

Mac OS上安装PostgreSQL的教程

今天我们来看在Mac OS上安装PostgreSQL的教程,这里我们通过brew包管理器来安装,所以首先我们会讲解brew的安装配置:
收藏 0 赞 0 分享

Linux CentOS 7安装PostgreSQL9.3图文教程

这篇文章主要为大家详细介绍了Linux CentOS 7安装PostgresSQL9.3图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Linux CentOS 7源码编译安装PostgreSQL9.5

这篇文章主要为大家详细介绍了Linux CentOS 7源码编译安装PostgreSQL9.5的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Mybatis调用PostgreSQL存储过程实现数组入参传递

这篇文章主要介绍了mybatis调用postgresql自定义函数传递数组参数的解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Windows PostgreSQL 安装图文教程

PostgreSQL是一套功能强大的对象-关系型数据库管理系统。经过十几年的发展, PostgreSQL 是世界上可以获得的最先进的开放源码的数据库系统
收藏 0 赞 0 分享

PostgreSQL 安装和简单使用第1/2页

ostgreSQL是现在比较流行的数据库之一,这个起源于伯克利(BSD)的数据库研究计划目前已经衍生成一项国际开发项目,并且有非常广泛的用户。
收藏 0 赞 0 分享
查看更多