Matching by ID

In addition to making patterns out of step patterns that specify an axis, node test, and predicate, you can also use the id() pattern to match elements that have a specific ID value. To use this pattern, you must give elements an ID attribute, and you must declare that attribute of type ID, as you can do in a DTD or schema. The following example rule adds the text of all elements that have the ID “favorite”:

<xsl:template match = "id('favorite')"> 
    <H3><xsl:value-of select="."/></H3> 
</xsl:template> 

Here’s what a DTD for planets.xml might look like that declares an ID and sets it to “favorite”:

 <?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="#stylesheet"?> <!DOCTYPE PLANETS [ <!ELEMENT PLANET (CUSTOMER)*> <!ELEMENT ...

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.