RefLib

Reference library for AI

Back

postgresql_16:3.6.15.2 XML Predicates

PostgreSQL 16 > The SQL Language > Functions and Operators > XML Functions > XML Predicates

The expressions described in this section check properties of xml values.

IS DOCUMENT

xml IS DOCUMENT boolean

The expression IS DOCUMENT returns true if the argument XML value is a proper XML document, false if it is not (that is, it is a content fragment), or null if the argument is null. See (see postgresql_16:3.5.13) about the difference between documents and content fragments.

IS NOT DOCUMENT

xml IS NOT DOCUMENT boolean

The expression IS NOT DOCUMENT returns false if the argument XML value is a proper XML document, true if it is not (that is, it is a content fragment), or null if the argument is null.

XMLEXISTS

XMLEXISTS ( text PASSING BY {REF|VALUE} xml BY {REF|VALUE} ) boolean

The function xmlexists evaluates an XPath 1.0 expression (the first argument), with the passed XML value as its context item. The function returns false if the result of that evaluation yields an empty node-set, true if it yields any other value. The function returns null if any argument is null. A nonnull value passed as the context item must be an XML document, not a content fragment or any non-XML value.

Example: SELECT xmlexists('//town[text() = ''Toronto'']' PASSING BY VALUE '<towns><town>Toronto</town><town>Ottawa</town></towns>'); xmlexists ------------ t (1 row)

The BY REF and BY VALUE clauses are accepted in PostgreSQL, but are ignored, as discussed in (see postgresql_16:9.4.3.2).

In the SQL standard, the xmlexists function evaluates an expression in the XML Query language, but PostgreSQL allows only an XPath 1.0 expression, as discussed in (see postgresql_16:9.4.3.1).

xml_is_well_formed

xml_is_well_formed ( text ) boolean
xml_is_well_formed_document ( text ) boolean
xml_is_well_formed_content ( text ) boolean

These functions check whether a text string represents well-formed XML, returning a Boolean result. xml_is_well_formed_document checks for a well-formed document, while xml_is_well_formed_content checks for well-formed content. xml_is_well_formed does the former if the (see guc-xmloption) configuration parameter is set to DOCUMENT, or the latter if it is set to CONTENT. This means that xml_is_well_formed is useful for seeing whether a simple cast to type xml will succeed, whereas the other two functions are useful for seeing whether the corresponding variants of XMLPARSE will succeed.

Examples: SET xmloption TO DOCUMENT; SELECT xml_is_well_formed('<>'); xml_is_well_formed -------------------- f (1 row) SELECT xml_is_well_formed('<abc/>'); xml_is_well_formed -------------------- t (1 row) SET xmloption TO CONTENT; SELECT xml_is_well_formed('abc'); xml_is_well_formed -------------------- t (1 row) SELECT xml_is_well_formed_document('<pg:foo xmlns:pg="http://postgresql.org/stuff">bar</pg:foo>'); xml_is_well_formed_document ----------------------------- t (1 row) SELECT xml_is_well_formed_document('<pg:foo xmlns:pg="http://postgresql.org/stuff">bar</my:foo>'); xml_is_well_formed_document ----------------------------- f (1 row) The last example shows that the checks include whether namespaces are correctly matched.

Back