O'Reilly logo

Firefox Hacks by Nigel McFarlane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #63. Use Client-Side XPath

Use XPath to enhance your XSL transformations.

XPath defines a syntax for selecting parts of an XML document. It consists of syntax to select direct elements and their attributes, as well as a set of standard functions. XPath was released as a W3C recommendation in 1999 and is such a major element of XSL that without it, you will not be able to make XSL documents. This hack serves to dirty your hands with basic XPath syntax.

XPath syntax is a superset of the standard patterns that are used to apply templates to XML elements using the match attribute of the xsl:template element. Patterns allow you to say which nodes you do and do not wish to match for a transformation. Basic patterns allow you to match nodes by element name, child elements, descendants, and attributes. The XPath superset allows matching on much more powerful expressions. These expressions can be used in any XSL element that allows a select attribute. Currently, that means these tags: xsl:apply-templates, xsl:value-of, xsl:for-each, xsl:copy-of, xsl:variable, xsl:param, and xsl:sort.

In addition to being able to select a list of element nodes, XPath is able to produce Booleans, numbers, and strings.

The power of XPath lies in its ability to match on all the basic match pattern tests, as well as on ancestor, parent, sibling, preceding, and following nodes. Without the XPath capabilities of XSL, performing anything other than simple operations on XML would be impossible.

Use Criteria to Select ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required