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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
2.94Mb size Format: txt, pdf, ePub

KindTests for Document Nodes

The
document-node() KindTes
t
can take an argument that is an
element()
or
schema-element(
)
KindTest
; it then matches any document node that has the specified kind of element as its only element child. For this
KindTest
to work, the document must be a well-formed document in the sense that the document node has exactly one element node as a child, and no text node children, and the element node must have been validated against a schema. Here are two examples:

Construct
Meaning
document-node
(
schema-element(mfg:invoice))
Matches the document node at the root of a well-formed document whose outermost element has been validated against the top-level element declaration named
invoice
in the schema for the namespace associated with the prefix
mfg
, or an element in the substitution group headed by the
invoice
element. This schema must have been imported into the stylesheet.
document-node
(
element(*, fin:movement))
Matches the document node at the root of a well-formed document whose outermost element has the type annotation
fin:movement
, or a type derived from this by restriction or extension. The schema for the namespace associated with the prefix
fin
must have been imported into the stylesheet.

Using KindTests

The simple
KindTests
comment()
,
processing-instruction()
, and
text()
are used whenever you want to match one of these kinds of node. These are used comparatively rarely. For example, it's unusual to define a template rule that matches text nodes: usually a stylesheet will either copy text nodes unchanged, or suppress them from the output, and the choice is usually controlled from the template rule for the containing element node.

The default template rule for comment nodes causes them to be discarded; if you want to copy comment nodes to the output, you can achieve this by adding the template rule.


However, this will work only if the template rule used to process the containing element issues the instruction

to process all its children.

The
[schema-]element()
and
[schema-]attribute() KindTests
are most useful when you a processing a source document that has been validated against a schema, especially when the schema is fairly complex. It allows you to define a generic rule for a whole class of elements or attributes. There are a number of ways such a class can be identified, and the approach you use will depend on the design of your schema:

  • Identifying the elements or attributes by type, using the syntax
    element(*, QName)
    or
    attribute(*, QName)
    is useful for processing elements and attributes that have simple content. For example, if all elements containing monetary amounts are identified as being of type
    money
    , then you can define a template rule with the pattern
    match = “element(*, money)”
    that contains the formatting logic for values of this type. This will also work if the schema defines subtypes derived by restricting the
    money
    type; for example, a subtype that restricts the values to be positive sums of money.
  • Where the schema defines many elements that share the same complex type, the syntax
    element(*, QName)
    can again be useful to define generic logic that applies to all elements of this type. For example, a schema for retail banking might define a generic type
    movement
    that represents all movements of money from one account to another. A template rule declared with
    match = “element(*, QName)”
    will match all elements declared with this type, or with a type derived from it.

Other books

Risk Everything by Sophia Johnson
War in Heaven by Gavin Smith
Nivel 26 by Anthony E. Zuiker
Sherlock Holmes by Dick Gillman
Shalia's Diary by Tracy St. John
Damage (Havoc #2) by Stella Rhys