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

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

The construct
node()
is the most general item type here: it matches any item that is a node.

Note, however, that if you use
node()
on its own as a step in a path expression or as an XSLT match pattern, it is short for
child::node()
and will only match nodes that are found on the child axis. The only nodes that can be found on the child axis are elements, text nodes, comments, and processing instructions.

The constructs
comment()
and
text()
are straightforward: they match comment nodes and text nodes, respectively.

For matching document nodes, you can write the test
document-node()
, which matches any document node, or you can be more specific. If you include an
ElementTest
within the parentheses, then this
ItemType
will only match a document node that satisfies the following two conditions:

  • The document node must be the root of a tree that corresponds to a well-formed XML document. Specifically this means that the document node must have exactly one element node, and no text nodes, among its children. It is allowed to have comments and processing instructions before or after the element node.
  • The element node that is a child of this document node must match the
    ElementTest
    given within the parentheses. The syntax for
    ElementTest
    is given in the next section.

This construct allows you to test what can be loosely called the “document type”, for example, you can test whether an input document returned by the
doc()
function is an invoice, by writing:

if (doc(“inv.xml”) instance of document-node(schema-element(mf:invoice))) …

This construct tests whether the document node is labeled as an invoice, as the result of previous validation. If the document has not been validated, the result will be false, whether or not validation would succeed if attempted. The way you control whether
doc()
invokes validation depends on the XSLT processor you are using. With Saxon, you can control it using the options
-val:lax
or
-val:strict
on the command line, or on a per-document basis using parameters appended to the URI. With AltovaXML, the document will be validated if it has an
xsi:schemaLocation
attribute.

Because the same syntax is used in XSLT patterns (described in the next chapter), you can also use it in template rules:

Other books

Touched by Cyn Balog
A Teenager's Journey by Richard B. Pelzer
Warlords Rising by Honor Raconteur
BoneMan's Daughters by Ted Dekker
Unraveled By The Rebel by Michelle Willingham
Dreamland by Sam Quinones
Never Say Never by Myburgh, Sonja