XSLT for Dummies

 
Chapter 11 - XPath Data Types and Functions
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

In addition to the built-in functions centered around data types, XSLT adds some general purpose functions for specific uses. I discuss these functions in this section.

Generating a unique ID string

You can generate a unique identifier for a node using generate-id () . This becomes useful when you need a unique value for each element in the result document. The XSLT processor generates the identifier during the transformation; the value of the identifier consists of any string of alphabetical or numeric characters , but it must start with an alphabetic character. The syntax for the function is:

string generate-id([nodeset])

The following example uses generate-id() to assign a value to a new id attribute and declare a new element:

<xsl:template match="entree"> <entree id="{generate-id()}" name="{@name}"><xsl:text> </xsl:text><xsl:element name="{generate- id()}"/><xsl:text> </xsl:text></entree> </xsl:template>

The results are:

<entree id="d0e3" name="Sunburnt Chicken"> <d0e3/> </entree> <entree id="d0e18" name="Filet Mig's None"> <d0e18/> </entree> <entree id="d0e33" name="Chicken Parmashaun"> <d0e33/> </entree> <entree id="d0e48" name="Eggs Benelux"> <d0e48/> </entree> <entree id="d0e63" name="Jerk Chicken"> <d0e63/> </entree> <entree id="d0e78" name="Gusto Spaghetti"> <d0e78/> </entree>

Pay special attention to the fact that, even though generate-id() is used twice in the template rule, the same ID value is generated for each of these times in each entree particular element node. The reason is that generate-id() creates a unique ID for a context node, so as long as the node stays in context, then the same value is returned.

 Tip   If you run this code more than once, youll likely get a different set of identifier values each time you run it, because the IDs are generated to be unique for a particular transformation. In fact, if you apply the same stylesheet twice, you will likely get different IDs both times. The important part is not the specific values themselves , but that they are unique within that particular transformation.

Also see Chapter 10 for another example of using generate-id() .

Returning system information

You can use the system-property () function to obtain certain system-level information, usually about the processor itself:

object system-property(string)

Each processor is required to support a minimum of three properties:

  • xsl:version returns a number indicating the version of XSLT that the processor implements. For example, for XSLT processors implementing XSLT 1.0, the number 1.0 is returned.

  • xsl:vendor returns a string that identifies the XSLT processor vendor.

  • xsl:vendor-url returns a string declaring a URL for the vendor of the XSLT processor.

The following stylesheet prints these three properties:

<xsl:template match="/"> XSLT Version Supported: <xsl:value-of select="system- property('xsl:version')"/><xsl:text> </xsl:text> XSLT Processor: <xsl:value-of select="system-property('xsl:vendor')"/><xsl:text> </xsl:text> For More Info: <xsl:value-of select="system-property('xsl:vendor-url')"/><xsl:text> </xsl:text> </xsl:template>

When run against the SAXON 6.4 processor, the following results are generated:

XSLT Version Supported: 1 XSLT Processor: SAXON 6.4.4 from Michael Kay For More Info: http://saxon.sourceforge.net

  
 
 
2000-2002    Feedback

Категории