XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (73 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4Mb size Format: txt, pdf, ePub
Construct
Meaning
element(*, mf:invoice)
An element node validated against the complex type
mf:invoice
defined in an imported schema
attribute(*, xs:NMTOKENS)
An attribute validated against the built-in schema list type
xs:NMTOKENS
document-node(element(*, mf:invoice))
A document node representing a well-formed XML document whose outermost element has been validated against the complex type
mf:invoice
defined in an imported schema

Often the structure of an element is defined in the schema not by creating an explicitly named

definition, but rather by means of an

declaration that contains an unnamed

. Here's a typical example, taken from the schema for XSLT 2.0 stylesheets:


  

    

      

        

          

        

      

    

  


To allow for the fact that many of the types defined in a typical schema are anonymous, there is another form of the
element()
test that is used to refer to elements conforming to this named element declaration. If you write:


then you are saying that the value of the parameter must be an element that has been validated against the element declaration
xsl:apply-imports
in this schema. The name of the element passed as the parameter must either be
xsl:apply-imports
, or must match an element defined in the schema to be a member of the substitution group of
xsl:apply-imports
.

You can similarly refer to top-level attribute declarations in the schema using the syntax
schema-attribute(nnn)
, though this form is not seen very often because it is unusual to find top-level attribute declarations in a schema.

Sequence type descriptors can also be used within XPath expressions. The expressions that use them are described in Chapter 11, which also defines the syntax and semantics in much greater detail than defined here. The operators that use sequence type descriptors are as follows:

  • instance of
    , which tests whether a given value is an instance of a given type. For example,
    @dob instance of attribute(*, xs:date)
    returns true if the attribute
    @dob
    is labeled with the type annotation
    xs:date
    (or a type derived from
    xs:date
    ), which will be true only if the attribute contains a valid date and has been validated using a schema that declares the type of the attribute as
    xs:date
    .
  • treat as
    , which asserts that a given value is an instance of a given type, causing a runtime failure if it is not. This operator is useful mainly with XPath processors that do strict static type-checking, which is unlikely to apply in an XSLT environment unless the processor has a diagnostic mode to do this extra level of checking.

Validating the Source Document

Validation is the process of taking a raw XML document and processing it using an XML Schema. The most obvious output of this process is a success or failure rating: the document is either valid or invalid against the schema. But this is not the only output. Validation also annotates the document, marking each element and attribute node with a label indicating its type. For example, if validation checks that a

element is valid according to the
us-postal-address
type in the schema, then this element will be annotated as having the type
us-postal-address
. There are various ways the stylesheet can then use this information:

Other books

A Merger by Marriage by Cat Schield
The Lovely Shadow by Cory Hiles
Article 5 by Kristen Simmons
Stolen Secrets by Nancy Radke
In Honor Bound by DeAnna Julie Dodson
Ashes by Now by Mark Timlin
Special Talents by J. B. Tilton
The Boy in the Suitcase by Lene Kaaberbol