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

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

In all these cases, a
NameTest
matches nodes only of the principal node kind for the selected axis. This means that if the
PatternAxis
is
@
or
attribute::
, the
NameTest
selects attribute nodes with the given name; otherwise, it matches element nodes only.

Patterns that match nodes by name are extremely common and work well with many kinds of document. If you find yourself writing a pattern with many alternatives, for example:

match=“b | i | u | sub | sup | s”

then (if you are using a schema-aware processor) it may be worth asking yourself what these elements have in common. One possibility is that they are all members of the same substitution group defined in the schema: in this case, you may be able to replace the pattern with one such as
match = “schema-element(inline)”
. Another possibility is that the elements all have the same internal structure. In this case they are likely to conform to the same type definition in the schema, so you can replace the list of elements by a pattern of the form
match = “element(*, inline-type)”
.

If the list includes most of the members of a substitution group, or most elements conforming to a given type, then you could consider excluding the unwanted ones with a predicate, for example:
match = “schema-element(inline)[not(self::schema-element(span))]”
. Alternatively, if the pattern is being used to define a template rule, simply define another template rule with higher priority to catch the exceptions.

A pattern of the form
prefix:*
, which matches all the elements (or attributes) in a particular namespace, is often useful if all that you want to do is exclude such elements from the result tree. An empty template rule takes the form:


This example causes all subtrees rooted at an element in the namespace
http://www.w3.org/2000/svg
to be excluded from the result. (This is the namespace for the W3 C Scalable Vector Graphics specification: see
http://www.w3.org/TR/SVG11/
).

*:local-name
patterns are new in XSLT 2.0. They should be used with care, because in principle the names in one namespace bear no relationship to names in a different namespace; for example, the
xsl:sequence
element in the XSLT namespace is quite unrelated to the
xs:sequence
element in the XML Schema namespace. However, there are cases where you might need to write a stylesheet that handles several namespaces that are variants of each other, in which many of the elements are common to more than one namespace. This can occur when matching (X)HTML, which may or may not use the XHTML namespace, or when matching RSS, which also exists in both namespace and no-namespace variants.

Other books

Beloved Wolf by Kasey Michaels
The Bumblebee Flies Anyway by Robert Cormier
Generation X by Douglas Coupland