XML(SEQUENCE)
to hold arbitrary pieces of XML content, PostgreSQL provides the single xml
type, which can hold “document” or “content”. There is no equivalent of the standard's “sequence” type.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.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.sequence
, which can contain XML nodes, atomic values, or both, does not exist in XPath 1.0. A 1.0 expression can only produce a node-set (containing zero or more XML nodes), or a single atomic value.boolean
, double
, and string
.if ( hat ) then hat/@size else "no hat"
has no XPath 1.0 equivalent."cat" < "dog"
and "cat" > "dog"
are false, because each is a numeric comparison of two NaN
s. In contrast, =
and !=
do compare the strings as strings.sale/@hatsize = 7
and sale/@customer = "alice"
are existentially quantified comparisons, true if there is any sale
with the given value for the attribute, but sale/@taxable = false()
is a value comparison to the effective boolean value of a whole node-set. It is true only if no sale
has a taxable
attribute at all.xml
value passed as the context item to any PostgreSQL XPath-based function must be in document form.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.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
is an XML value, these two queries in an SQL:2006 environment would produce true and false, respectively: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
.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.