How to Use SQL Predicates with XML
Predicates return a value of True or False. Some new predicates have been added to SQL that specifically relate to XML. This can be helpful in significantly reducing your workload.
The purpose of the DOCUMENT predicate is to determine whether an XML value is an XML document. It tests to see whether an XML value is an instance of either XML(ANY DOCUMENT) or XML(UNTYPED DOCUMENT). The syntax is:
XML-value IS [NOT] [ANY | UNTYPED] DOCUMENT
If the expression evaluates to True, the predicate returns TRUE; otherwise, it returns FALSE. If the XML value is null, the predicate returns an UNKNOWN value. If you don’t specify either ANY or UNTYPED, the default assumption is ANY.
You use the CONTENT predicate to determine whether an XML value is an instance of XML(ANY CONTENT) or XML(UNTYPED CONTENT). Here’s the syntax:
XML-value IS [NOT] [ANY | UNTYPED] CONTENT
If you don’t specify either ANY or UNTYPED, ANY is the default.
As the name implies, you can use the XMLEXISTS predicate to determine whether a value exists. Here’s the syntax:
XMLEXISTS ( XQuery-expression [ argument-list ])
The XQuery expression is evaluated using the values provided in the argument list. If the value queried by the XQuery expression is the SQL NULL value, the predicate’s result is unknown. If the evaluation returns an empty XQuery sequence, the predicate’s result is FALSE; otherwise, it’s TRUE.
You can use this predicate to determine whether an XML document contains particular content before you use a portion of that content in an expression.
The VALID predicate is used to evaluate an XML value to see whether it is valid in the context of a registered XML Schema. The syntax of the VALID predicate is more complex than is the case for most predicates:
xml-value IS [NOT] VALID [XML valid identity constraint option] [XML valid according-to clause]
This predicate checks to see whether the XML value is one of the five XML subtypes: XML(SEQUENCE), XML(ANY CONTENT), XML(UNTYPED CONTENT), XML(ANY DOCUMENT), or XML(UNTYPED DOCUMENT). Additionally, it might optionally check to see whether the validity of the XML value depends on identity constraints, and whether it is valid with respect to a particular XML Schema (the validity target).
There are four possibilities for the identity-constraint-option component of the syntax:
WITHOUT IDENTITY CONSTRAINTS: If the identity-constraint-option syntax component isn’t specified, WITHOUT IDENTITY CONSTRAINTS is assumed. If DOCUMENT is specified, then it acts like a combination of the DOCUMENT predicate and the VALID predicate WITH IDENTITY CONSTRAINTS GLOBAL.
WITH IDENTITY CONSTRAINTS GLOBAL: This component of the syntax means the value is checked not only against the XML Schema, but also against the XML rules for ID/IDREF relationships.
ID and IDREF are XML attribute types that identify elements of a document.
WITH IDENTITY CONSTRAINTS LOCAL: This component of the syntax means the value is checked against the XML Schema but not against the XML rules for ID/IDREF or the XML Schema rules for identity constraints.
DOCUMENT: This component of the syntax means the XML value expression is a document and is valid WITH IDENTITY CONSTRAINTS GLOBAL syntax with an XML valid according to clause. The XML valid according to clause identifies the schema that the value will be validated against.