The <xsl:key> Element: Matching by Key

You use the <xsl:key> element when you want to create a key and match nodes using that key. The <xsl:key> element has the following attributes:

  • name(mandatory). The name of the key; set to a QName.

  • match(mandatory). Set to a match pattern to match the nodes to which this key applies.

  • use(mandatory). Set to an expression used to determine the value of the key.

Keys give you an easy way to identify elements, and you can match specific keys with the pattern “key()”. Chapter 4 gave you an introduction to single-valued keys. In that example, I used a key to match planets whose COLOR attribute was set to “BLUE”, which in that case was the Earth:

 <?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="planets.xsl"?> ...

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.