Singleton checks
statementsExample: Known singleton
When reporting errors,
data type methods raise a single error in the following format:
For example:
Location steps, function parameters, and operators that require singletons will return an error if
the compiler can’t determine whether a singleton is guaranteed at run time. This problem
occurs frequently with untyped data. For example, the lookup of an attribute requires a
singleton parent element. An ordinal that selects a single parent node is sufficient. The
evaluation of a
combination to extract attribute values might not require the
ordinal specification. This is shown in the next example.
In this example, the
method generates a separate row for each
element. The
method that is evaluated on a
node extracts the value of
and, being an
attribute, is a singleton.
7
Note
Parsing errors raised by the XQuery parser (such as syntax errors in the XML referenced as
part of the XML data type method, for example), abort the active transaction, regardless of
the
setting of the current session.
value()
Example: Using value()
XML schema is used for type checking of typed XML. If a node is specified as a singleton in the
XML schema, the compiler uses that information and no error occurs. Otherwise, an ordinal
that selects a single node is required. In particular, the use of descendant-or-self axis (//) axis,
such as in
, loses singleton cardinality inference for the
element, even if
the XML schema specifies it to be so. Therefore, you should rewrite it as.
It’s important to remain aware of the difference between
and
for type checking. The former returns a sequence of
nodes in which each
node is the leftmost
node among its siblings. The latter returns the first singleton
node in document order in the XML instance.
The following query on an untyped XML column results in a static, compilation error. This is
because
expects a singleton node as the first argument and the compiler can’t
determine whether only one
node will occur at run time:
Following is a solution that you could consider:
However, this solution doesn’t solve the error, because multiple
nodes might occur in
each XML instance. The following rewrite works:
This query returns the value of the first
element in each XML instance.
xml Data Type Methods