Chapter 11 showed how sequence types are used to match sequences in various expressions, including function calls. When schemas have been imported into a query, additional tests are available for sequence types, including testing for name and type. Their syntax is shown in Figure 13-4. These tests can be used not just in sequence types but also as kind tests in path expressions.
Chapter 11 introduced the element( )
and attribute( )
tests. For example, you can use the test element(prod:product)
to test for elements whose name is prod:product
.
These tests can also be used with user-defined types. For example, the sequence type:
element(prod:product, prod:ProductType)
matches an element whose name is prod:product
and that has the type prod:ProductType
, or any type derived by restriction or extension from prod:ProductType
. Yet another syntax is:
element(*, prod:ProductType)
which matches any element that has the type prod:productType
(or a derived type), regardless of name. Note that the element must already have been validated and annotated with the type prod:ProductType
. It is not enough that it would be a valid instance of that type if it were validated.
You can also match an element or attribute based on its name using the schema-element( )
and schema-attribute( )
tests. For example, you can use the sequence type schema-element(prod:product)
to match only elements whose qualified name is prod:product
. This differs from the element(prod:product)
syntax in that the name must be among the globally declared element or attribute names in the ISSD. Also, in order to match, a node must have been validated according to that declaration.
Another difference is that schema-element(prod:product)
will also match elements that are in the substitution group of product
. Substitution groups are a feature of XML Schema that allows you to specify that certain elements are equivalent. For example, you might put elements shirt
, hat
, and suitcase
in the substitution group headed by the product
element. These three elements can then appear in content anywhere a product
element may appear. The sequence type schema-element(prod:product)
would then match shirt
, hat
, and suitcase
elements in addition to product
elements.
For attributes, you can specify schema-attribute
and attribute
and the same rules apply. However, these constructs are rarely used because attributes don't have substitution groups,
and global attribute declarations are quite rare. Table 13-3 shows some examples.
Table 13-3. Examples of sequence types based on name and type
Example |
Explanation |
---|---|
schema-element (product)+ |
One or more elements whose qualified name is |
schema-element (prod:product) |
One element whose qualified name is equal to |
schema-attribute (prod:dept) |
One attribute whose qualified name is |
element(*, prod:ProductType) |
One element whose type annotation is |
element (prod:product, prod:ProductType) |
One element whose qualified name is equal to |
You can use a question mark at the end of the type name in an element sequence type. This means that if an element is nilled (i.e., it has the attribute xsi:nil
set to true
), it can match the sequence type even though it does not have any content. For example, element(product, ProductType?)
matches both a regular product
element and a nilled product
element such as <product xsi:nil="true"/>
. Note that this is different from a question mark at the very end of the sequence type, which indicates that the empty sequence should match.
3.135.190.182