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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.