PostgreSQL中的XML操作函数代码

 更新时间:2009年09月06日 01:03:22   作者:  
PostgreSQL中的XML操作函数代码

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 中的加密扩展插件pgcrypto用法说明

    postgresql 中的加密扩展插件pgcrypto用法说明

    这篇文章主要介绍了postgresql 中的加密扩展插件pgcrypto用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • 详解如何定位postgreSQL数据库中未被使用过的索引

    详解如何定位postgreSQL数据库中未被使用过的索引

    在生产环境上,由于不规范的优化措施,数据库中可能存在大量的索引,并且相当一部分的索引重未被使用过,今天带大家如何找出这些索引,本文给大家介绍了定位postgreSQL数据库中未被使用过的索引的方法,需要的朋友可以参考下
    2024-03-03
  • PostgreSql 的hash_code函数的用法说明

    PostgreSql 的hash_code函数的用法说明

    这篇文章主要介绍了PostgreSql 的hash_code函数的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • 基于PostgreSQL 权限解读

    基于PostgreSQL 权限解读

    这篇文章主要介绍了基于PostgreSQL 权限解读,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • 关于PostgreSQL截取某个字段中的部分内容进行排序的问题

    关于PostgreSQL截取某个字段中的部分内容进行排序的问题

    这篇文章主要介绍了PostgreSQL截取某个字段中的部分内容进行排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • PostgreSQL limit的神奇作用详解

    PostgreSQL limit的神奇作用详解

    这篇文章主要介绍了PostgreSQL limit的神奇作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-09-09
  • postgresql数据库连接数和状态查询操作

    postgresql数据库连接数和状态查询操作

    这篇文章主要介绍了postgresql数据库连接数和状态查询操作,具有很好的参考价值,对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • postgresql数据库导出和导入及常用的数据库管理工具

    postgresql数据库导出和导入及常用的数据库管理工具

    数据库的导入导出是最常用的功能之一,而PostgreSQL提供的对应工具为pg_dump和pg_restore。pg_dump是用于备份PostgreSQL数据库的工具,下面这篇文章主要给大家介绍了关于postgresql数据库导出和导入及常用的数据库管理工具使用的相关资料,需要的朋友可以参考下
    2023-04-04
  • 基于postgreSql 常用查询小结

    基于postgreSql 常用查询小结

    这篇文章主要介绍了基于postgreSql 常用查询小结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Postgresql 截取字符串的案例

    Postgresql 截取字符串的案例

    这篇文章主要介绍了Postgresql 截取字符串的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02

最新评论