金仓数据库KingbaseES XML数据类型介绍
关键字:
KingbaseES、XML、XML数据类型、XML实体、人大金仓
一、XML类型数据输入
xml数据类型可以被用来存储XML数据。它比直接在一个text域中存储XML数据的优势在于, 它会检查输入值的结构是不是良好,并且有支持函数用于在其上执行类型安全的操作。使用这种数据类型要求在安装时用configure --with-libxml选项编译。
与其他类型类似,可以使用下面两种语法来输入XML类型的数据:
xml '<test>hello world</test>'
'<test>hello world</test>'::xml;
示例如下:
test=# select xml '<test>hello</test>';
xml
--------------------
<test>hello</test>
(1 行记录)
test=# select '<test>hello world</test>'::xml;
xml
--------------------------
<test>hello world</test>
(1 行记录)
xml类型可以存储遵循XML标准定义的“documents”,以及“content”片段,它是通过引用XQuery 和 XPath 数据模型来定义的。
“content”片段可以有多个顶级元素或“character”节点,但“documents”只能有一个顶级元素。可以使用表达式xmlvalue IS DOCUMENT来评估一个特定的xml值是一个“documents”还是“content”片段。Xmloptions用来指定输入的数据是documents”还是“content”片段,KingbaseES默认情况下此值为“content”片段。所以输入的xml可以有多个顶级元素,但是如果把此参数值改为documents”,将不能输入多个顶级元素的内容。示例如下:
test=# show xmloption;
xmloption
-----------
content
(1 行记录)
test=# select xml '<a>a</a><b>b</b>';
xml
------------------
<a>a</a><b>b</b>
(1 行记录)
test=# set xmloption to document;
SET
test=# select xml '<a>a</a><b>b</b>';
ERROR: invalid XML document
第1行select xml '<a>a</a><b>b</b>';
^
描述: line 1: Extra content at the end of the document
<a>a</a><b>b</b>
^
也可以通过xmlparse函数从字符数据中生成一个 xml 类型的值,xmlparse函数的语法为:XMLPARSE ( { DOCUMENT | CONTENT } value)。此函数的DOCUMENT和CONTENT表示XML数据的类型,示例如下:
test=# select XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>doc</chapter></book>');
xmlparse
----------------------------------------------------------
<book><title>Manual</title><chapter>doc</chapter></book>
(1 行记录)
test=# select XMLPARSE (CONTENT '<?xml version="1.0"?><title>Manual</title><chapter>doc</chapter>');
xmlparse
---------------------------------------------
<title>Manual</title><chapter>doc</chapter>
(1 行记录)
test=# select XMLPARSE (DOCUMENT '<?xml version="1.0"?><title>Manual</title><chapter>doc</chapter>');
ERROR: invalid XML document
描述: line 1: Extra content at the end of the document
<?xml version="1.0"?><title>Manual</title><chapter>doc</chapter>
^
此外,在 KingbaseES 中处理 XML 数据产生错误的可能性更小,并且在 XML 数据编码、客户端编码和服务器编码三者相同时效率更高。因为 XML 数据在内部是以 UTF-8 处理的,如果服务器编码也是 UTF-8 时,计算效率将会最高。
当服务器编码不是 UTF-8 时,某些 XML 相关的函数可能在非 ASCII 数据上完全无法工作。尤其在 xmltable() 和xpath() 上,这是一个已知的问题。
二、XML实体引用
在XML中,一些字符拥有特殊的意义。
如果你把字符“<”放在XML元素中,会发生错误,这是因为解析器会把它当作新元素的开始。
这样会产生XML错误:
test=# select xml '<message>if salary < 1000 then</message>';
ERROR: invalid XML document
第1行select xml '<message>if salary < 1000 then</message>';
^
描述: line 1: StartTag: invalid element name
<message>if salary < 1000 then</message>
^
为了避免这个错误,请用实体引用来代替“<”字符:
test=# select xml '<message>if salary < 1000 then</message>';
xml
---------------------------------------------
<message>if salary < 1000 then</message>
(1 行记录)
在XML中,有5个预定义的实体引用:
< | < | 小于 |
> | > | 大于 |
& | & | 和号 |
' | ‘ | 单引号 |
" | “ | 引号 |
注释:在XML中,只有字符“<”和“&”确实是非法的,大于号、单引号和引号是合法的,但是用实体引用来代替它是一个好习惯。
test=# select xml '<message>if salary & 1000 then</message>';
ERROR: invalid XML content
第1行select xml '<message>if salary & 1000 then</message>';
^
描述: line 1: xmlParseEntityRef: no name
<message>if salary & 1000 then</message>
^
line 1: chunk is not well balanced
<message>if salary & 1000 then</message>
^
test=# select xml '<message>if salary '' 1000 then</message>';
xml
------------------------------------------
<message>if salary ' 1000 then</message>
(1 行记录)
test=# select xml '<message>if salary " 1000 then</message>';
xml
------------------------------------------
<message>if salary " 1000 then</message>
(1 行记录)
三、XML中的注释
在Kingbase中,生成XML注释有一个函数:xmlcomment。
- 语法:
xmlcomment(text)
- 用法说明:
函数xmlcomment创建了一个 XML 值,它包含一个使用指定文本作为内容的 XML 注释。该文本不包含“--”或者也不会以一个“-”结尾,这样结果的结构是一个合法的 XML 注释。如果参数为空,结果也为空。
- 示例:
test=# SELECT xmlcomment('hello');
xmlcomment
--------------
<!--hello-->
(1 行记录)