5.2 Node-Set Functions

The node-set functions return node-sets, strings, or numbers. From XPath, the node-set functions include count( ), id( ), last( ), local-name( ), name( ), namespace-uri( ), and position( ). The count( ), last( ), and position( ) functions return numbers, and local-name( ), name( ), and namespace-uri( ) return strings. The following additional functions from XSLT return node-sets: document( ), key( ), and current( ).

You saw the name( ) and local-name( ) functions several times in earlier chapters, and you'll see it in later chapters, too. You'll get a chance to explore the document( ) function in Chapter 13 and the key( ) function in Chapter 11. The current( ) function is used in an example in Section 5.4, later in this chapter.

5.2.1 The id( ) Function

Here's how the id( ) function works. In valid XML 1.0, an ID is a unique identifier held in an attribute value. The attribute must be declared in a DTD to be recognized as being of type ID. If this identifier of type ID is duplicated in the document, the document is invalid. An ID must not start with a number. IDs can uniquely identify an element, which helps you to find and isolate parts of documents that may be of interest.

A document you saw in the last chapter, provinces.xml, has id attributes of type ID. The id( ) function can help you find a given ID in a document and then return some bit of information. The stylesheet id.xsl uses id( ):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>     <xsl:template match="provinces">  <xsl:apply-templates select="id('NU')"/> </xsl:template>     <xsl:template match="id('NU')">  <xsl:value-of select="name"/> </xsl:template>     </xsl:stylesheet>

The id( ) function may be used in patterns as well as expressions. The pattern that matches id('NU') finds an element node with an attribute of type ID that has a value of NU. The following is a fragment from provinces.xml:

<province >  <name>Nunavut</name>  <abbreviation>NU</abbreviation> </province>

When applied to provinces.xml, id.xsl finds this instance of the province element and returns the string value of the text in the name element that is a child of province. This is what the processor will return:

Nunavut

Now, I'll introduce some number functions.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net