Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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: