Postgresql 中文操作指南
D.3. XML Limits and Conformance to SQL/XML #
SQL:2006 引入了对 ISO/IEC 9075-14 (SQL/XML) 中的 XML 相关规范的重大修订。PostgreSQL 对 XML 数据类型的实现以及相关的函数主要遵循早期的 2003 年版本,并从较晚的版本中借用了部分内容。特别是:
Significant revisions to the XML-related specifications in ISO/IEC 9075-14 (SQL/XML) were introduced with SQL:2006. PostgreSQL’s implementation of the XML data type and related functions largely follows the earlier 2003 edition, with some borrowing from later editions. In particular:
此部分介绍部分可能会遇到的差异结果。
This section presents some of the resulting differences you may encounter.
D.3.1. Queries Are Restricted to XPath 1.0 #
PostgreSQL 专有函数 xpath() 和 xpath_exists() 使用 XPath 语言查询 XML 文档。PostgreSQL 还提供标准函数 XMLEXISTS 和 XMLTABLE 的仅 XPath 变体,官方使用 XQuery 语言。对于所有这些函数,PostgreSQL 都依赖于仅提供 XPath 1.0 的 libxml2 库。
The PostgreSQL-specific functions xpath() and xpath_exists() query XML documents using the XPath language. PostgreSQL also provides XPath-only variants of the standard functions XMLEXISTS and XMLTABLE, which officially use the XQuery language. For all of these functions, PostgreSQL relies on the libxml2 library, which provides only XPath 1.0.
XQuery 语言与 XPath 2.0 及更高版本之间有强关联性:任何在两者中都语法有效且成功执行的表达式都产生相同结果(包含数字字符引用或预定义实体引用的表达式除外,一个次要例外是,XQuery 用相应字符替换它们,而 XPath 将它们单独保留)。但这些语言与 XPath 1.0 之间没有这种关联性;XPath 1.0 是一种较早的语言,在许多方面都不同。
There is a strong connection between the XQuery language and XPath versions 2.0 and later: any expression that is syntactically valid and executes successfully in both produces the same result (with a minor exception for expressions containing numeric character references or predefined entity references, which XQuery replaces with the corresponding character while XPath leaves them alone). But there is no such connection between these languages and XPath 1.0; it was an earlier language and differs in many respects.
有两类限制需要记住:针对 SQL 标准中指定函数将 XQuery 限制为 XPath,以及针对标准函数和 PostgreSQL 专有函数将 XPath 限制为 1.0 版。
There are two categories of limitation to keep in mind: the restriction from XQuery to XPath for the functions specified in the SQL standard, and the restriction of XPath to version 1.0 for both the standard and the PostgreSQL-specific functions.
D.3.1.1. Restriction of XQuery to XPath #
XQuery 优于 XPath 的特性包括:
Features of XQuery beyond those of XPath include:
最近的 XPath 版本开始提供与这些功能重叠的能力(例如功能风格 for-each 和 sort、匿名函数,以及 parse-xml 从字符串中创建节点),但此类功能在 XPath 3.0 之前不可用。
Recent XPath versions begin to offer capabilities overlapping with these (such as functional-style for-each and sort, anonymous functions, and parse-xml to create a node from a string), but such features were not available before XPath 3.0.
D.3.1.2. Restriction of XPath to 1.0 #
对于熟悉 XQuery 和 XPath 2.0 或更高版本的开发人员,XPath 1.0 提出了许多差异需要解决:
For developers familiar with XQuery and XPath 2.0 or later, XPath 1.0 presents a number of differences to contend with:
Note
libxml2 库似乎总是将节点集返回给 PostgreSQL,且其成员与输入文档中的相对顺序相同。它的文档没有包含此行为,并且 XPath 1.0 表达式无法控制它。
The libxml2 library does seem to always return node-sets to PostgreSQL with their members in the same relative order they had in the input document. Its documentation does not commit to this behavior, and an XPath 1.0 expression cannot control it.
此处突出显示的区别并不是全部。在 XQuery 和 XPath 的 2.0 及更高版本中,存在 XPath 1.0 兼容性模式,W3C 中列出的 function library changes 和 language changes 在该模式中应用,提供了更完整(但仍然不详尽)的区别说明。兼容性模式无法使更高版本的语言与 XPath 1.0 完全等效。
The differences highlighted here are not all of them. In XQuery and the 2.0 and later versions of XPath, there is an XPath 1.0 compatibility mode, and the W3C lists of function library changes and language changes applied in that mode offer a more complete (but still not exhaustive) account of the differences. The compatibility mode cannot make the later languages exactly equivalent to XPath 1.0.
D.3.1.3. Mappings between SQL and XML Data Types and Values #
在 SQL:2006 及更高版本中,标准 SQL 数据类型与 XML Schema 类型之间的转换的两个方向都得到了精确指定。但是,这些规则是用 XQuery/XPath 的类型和语义来表达的,并且不直接适用于 XPath 1.0 的不同数据模型。
In SQL:2006 and later, both directions of conversion between standard SQL data types and the XML Schema types are specified precisely. However, the rules are expressed using the types and semantics of XQuery/XPath, and have no direct application to the different data model of XPath 1.0.
当 PostgreSQL 将 SQL 数据值映射到 XML(如 xmlelement 所示)时,或将 XML 映射到 SQL(如 xmltable 的输出列中所述),除了经过特殊处理的少数情况外,PostgreSQL 只是简单地假设 XML 数据类型的 XPath 1.0 字符串形式将作为 SQL 数据类型的文本输入形式有效,反之亦然。此规则具有简单性的优点,同时对许多数据类型产生类似于标准中指定映射的结果。
When PostgreSQL maps SQL data values to XML (as in xmlelement), or XML to SQL (as in the output columns of xmltable), except for a few cases treated specially, PostgreSQL simply assumes that the XML data type’s XPath 1.0 string form will be valid as the text-input form of the SQL datatype, and conversely. This rule has the virtue of simplicity while producing, for many data types, results similar to the mappings specified in the standard.
在与其他系统互操作是个问题的地方,对于某些数据类型,可能需要显式使用数据类型格式化函数(例如 Section 9.8 中的那些函数)来生成标准映射。
Where interoperability with other systems is a concern, for some data types, it may be necessary to use data type formatting functions (such as those in Section 9.8) explicitly to produce the standard mappings.
D.3.2. Incidental Limits of the Implementation #
本部分涉及的限制不是 libxml2 库固有的,而是适用于 PostgreSQL 中的当前实现。
This section concerns limits that are not inherent in the libxml2 library, but apply to the current implementation in PostgreSQL.
D.3.2.1. Only BY VALUE Passing Mechanism Is Supported #
SQL 标准定义了两个 passing mechanisms,当从 SQL 传递 XML 参数到 XML 函数或接收结果时适用:BY REF,其中特定 XML 值保留其节点标识;以及 BY VALUE,其中传递 XML 的内容,但节点标识不会保留。可在参数列表前指定机制,作为所有参数的默认机制,或在任何参数后指定机制,以覆盖默认值。
The SQL standard defines two passing mechanisms that apply when passing an XML argument from SQL to an XML function or receiving a result: BY REF, in which a particular XML value retains its node identity, and BY VALUE, in which the content of the XML is passed but node identity is not preserved. A mechanism can be specified before a list of parameters, as the default mechanism for all of them, or after any parameter, to override the default.
为了说明差异,如果 x 是一个 XML 值,那么在 SQL:2006 环境中的这两个查询将分别产生真和假:
To illustrate the difference, if x is an XML value, these two queries in an SQL:2006 environment would produce true and false, respectively:
SELECT XMLQUERY('$a is $b' PASSING BY REF x AS a, x AS b NULL ON EMPTY);
SELECT XMLQUERY('$a is $b' PASSING BY VALUE x AS a, x AS b NULL ON EMPTY);
PostgreSQL 将在 XMLEXISTS 或 XMLTABLE 结构中接受 BY VALUE 或 BY REF,但会忽略它们。xml 数据类型保存序列化字符串表示形式,因此没有节点标识需要保留,并且传递始终有效 BY VALUE。
PostgreSQL will accept BY VALUE or BY REF in an XMLEXISTS or XMLTABLE construct, but it ignores them. The xml data type holds a character-string serialized representation, so there is no node identity to preserve, and passing is always effectively BY VALUE.
D.3.2.2. Cannot Pass Named Parameters to Queries #
基于 XPath 的函数支持传递一个参数作为 XPath 表达式的上下文项目,但不支持传递附加值作为命名参数提供给表达式。
The XPath-based functions support passing one parameter to serve as the XPath expression’s context item, but do not support passing additional values to be available to the expression as named parameters.
D.3.2.3. No XML(SEQUENCE) Type #
PostgreSQL xml 数据类型只能保留 DOCUMENT 或 CONTENT 形式的值。XQuery/XPath 表达式上下文项目必须是单个 XML 节点或原子值,但 XPath 1.0 进一步将其限制为仅是 XML 节点,并且没有允许 CONTENT 的节点类型。结果是,格式良好的 DOCUMENT 是 PostgreSQL 可以作为 XPath 上下文项目提供的 XML 值的唯一形式。
The PostgreSQL xml data type can only hold a value in DOCUMENT or CONTENT form. An XQuery/XPath expression context item must be a single XML node or atomic value, but XPath 1.0 further restricts it to be only an XML node, and has no node type allowing CONTENT. The upshot is that a well-formed DOCUMENT is the only form of XML value that PostgreSQL can supply as an XPath context item.