Sequence Types and Schemas

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.

Element and attribute tests (for sequence types and kind tests)

Figure 13-4. Element and attribute tests (for sequence types and kind tests)

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 product, that have been validated using a global element declaration in the ISSD

schema-element (prod:product)

One element whose qualified name is equal to prod:product, that has been validated using a global element declaration in the ISSD

schema-attribute (prod:dept)

One attribute whose qualified name is prod:dept, that has been validated using a global attribute declaration in the ISSD

element(*, prod:ProductType)

One element whose type annotation is prod:ProductType or a type derived from prod:ProductType

element (prod:product, prod:ProductType)

One element whose qualified name is equal to prod:product, whose type annotation is prod:ProductType or a type derived from prod:ProductType, that is in the ISSD

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.190.182