Pattern Examples

The best way to understand patterns is by example. Suppose that you want to transform planets.xml into planets.html, but retain only the first planet, Mercury. You can do that with the predicate [position() < 2], because the first planet’s position is 1, the next is 2, and so on. Note, however, that < is invariably a sensitive character for XSLT processors, because it’s what you use to start markup; rather than <, you should use &lt.;. And note that you have to strip the other elements out of planets.xml by supplying an empty template for them, which I can do with the predicate [position() >= 2]:

Listing 4.10. Retaining Only Mercury
 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> ...

Get Inside XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.