分享好友 数据库首页 频道列表

PostgreSQL中的XML操作函数代码

PostgreSQL  2015-07-03 11:050

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)

查看更多关于【PostgreSQL】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Mac OS上安装PostgreSQL的教程
今天我们来看在Mac OS上安装PostgreSQL的教程,这里我们通过brew包管理器来安装,所以首先我们会讲解brew的安装配置:

0评论2016-06-26573

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

0评论2016-05-26227

探讨select in 在postgresql的效率问题
这篇文章主要介绍了探讨select in 在postgresql的效率问题 的相关资料,需要的朋友可以参考下

0评论2016-04-28164

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

0评论2015-08-12237

解决PostgreSQL服务启动后占用100%CPU卡死的问题
前文书说到,今天耗费了九牛二虎之力,终于驯服了NTFS权限安装好了PostgreSQL,却不曾想,服务启动后,新的状况又出现了。

0评论2015-07-03208

PostgreSQL中的OID和XID说明
在PostgreSQL中经常碰到OID和XID,刚才不明白这些东西是干什么的。

0评论2015-07-03196

PostgreSQL数据库性能提升的几个方面
PostgreSQL提供了一些帮助提升性能的功能。主要有一些几个方面。

0评论2015-07-03201

Debian中PostgreSQL数据库安装配置实例
这篇文章主要介绍了Debian中PostgreSQL数据库安装配置实例,一个简明教程,需要的朋友可以参考下

0评论2015-07-03203

PostgreSQL新手入门教程
本文介绍PostgreSQL的安装和基本用法,供初次使用者上手。以下内容基于Debian操作系统,其他操作系统实在没有精力兼顾,但是大部分内容应该普遍适用

0评论2015-07-03251

phpPgAdmin常见错误和问题的解决办法
这篇文章主要介绍了phpPgAdmin 常见错误和问题的解决办法,如安装错误、登陆错误、转储功能、其它错误和问题等,需要的朋友可以参考下

0评论2015-07-03178

PostgreSQL创建表分区
在pg里表分区是通过表继承来实现的,一般都是建立一个主表,里面是空,然后每个分区都去继承它。

0评论2015-07-03251

用PostgreSQL数据库做地理位置app应用
项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇 英文的、讲的很好的文章,特此翻译,希望能够帮助到以后用到earthdistance的同学

0评论2015-07-03176

PostgreSQL管理工具phpPgAdmin入门指南
phpPgAdmin是用PHP开发的一个基于web的PostgreSQL数据库管理工具。和MySql时代的PHPMyAdmin类似。本文介绍了phpPgAdmin安装和使用方法,需要的朋友可以参考下

0评论2015-07-03180

15个postgresql数据库实用命令分享
这篇文章主要介绍了15个实用的postgresql数据库命令分享,都是一些技巧性的postgresql命令,需要的朋友可以参考下

0评论2015-07-03206

PostgreSQL查看数据库,索引,表,表空间大小的示例代码
PostgreSQL 提供了多个系统管理函数来查看表,索引,表空间及数据库的大小,下面详细介绍一下

0评论2015-07-03215

更多推荐