본문 바로가기

카테고리 없음

Xsl Key Generate Id Example



  1. Xsl Key Function
  2. Key Generator
  3. Xml Xsl Examples
  4. Xsl Generate-id
  5. Xsl Generate Id
  6. Xslt Generate-id Key Example

Definition and Usage. The xsl:key element is a top-level element which declares a named key that can be used in the style sheet with the key function.

Key Basics

XSLT keys provide a simple and efficient way of looking up values based on other values. First, we will take a look at the syntax of keys and then we will look at how they are used in practice.

<xsl:key/>

Keys are created with the <xsl:key> tag, which must be a child of <xsl:stylesheet/>. It has three required attributes shown in the table below.

xsl:key Attributes
Attribute Description
name Name of the key.
match Pattern to match.
use The part of the matched node that will serve as the look-up index.

The key() Function

The key() function is used to look up the node or nodes specified by the key. It takes two arguments: the name of the key and the index.

Xsl Key Function

The example below shows how to create a key and then look up a value in the key.

Code Sample:

Generate

Code Sample:

Keys/Demos/SimpleKey.xsl

Key Generator

Xml xsl examples

In the example above, the key is created with this line:

Xml Xsl Examples

<xsl:key name='keyBeatle' match='beatle' use='name/firstname'/>

Xsl Generate-id

Imagine that it creates a lookup table that looks something like this:

Xsl Generate Id

Index Node-set
Paul Paul McCartney beatle node
John John Lennon beatle node
George George beatle node
Ringo Ringo beatle node

The key() function is used to look up the node or nodes specified by the key. It takes two arguments: the name of the key and the index.

<xsl:value-of select='key('keyBeatle',$FirstName)/@link'/>

Xslt Generate-id Key Example

As $Firstname is set to 'John' in the xsl:param, this line above returns 'http://www.johnlennon.com'. As you can see, the key() function returns nodes in the XML document. We can use XPath to drill down further into these nodes. In the example above, we drill down to the link attribute of the beatle node returned by key().