5.1 Boolean Functions

Boolean functions return true or false and are used in stylesheet logic that tests whether something is true. In XPath, the Boolean functions are boolean( ), false( ), lang( ), not( ), and true( ). In XSLT, the functions that return Booleans are element-available( ) and function-available( ). You'll learn to use element-available( ) and function-available( ) in Chapter 15, as well as how to use the system-property( ) function that returns an object. The strings functions contains( ) and starts-with( ) also return Booleans.

5.1.1 The lang( ) Function

The XPath Boolean functions include lang( ), which returns true if an xml:lang attribute exists with a given language token its only argument. This token must be quoted with single quotes. (By the way, the value of xml:lang is inherited by child elements, though this isn't shown in the following example.)

The brief document greet.xml in examples/ch05 contains greetings in four languages English, French, Spanish, and German:

<?xml version="1.0"?>     <greet>  <greeting xml:lang="en">Welcome</greeting>  <greeting xml:lang="fr">Bienvenue</greeting>  <greeting xml:lang="es">Bienvenida</greeting>  <greeting xml:lang="de">Willkommen</greeting> </greet>

The stylesheet greet.xsl uses the lang( ) function to select the greeting element that has an xml:lang attribute with a value of fr:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/>     <xsl:template match="greet">  <xsl:apply-templates select="greeting[lang('fr')]"/> </xsl:template>     <xsl:template match="greeting[lang('fr')]">  <xsl:text>French: </xsl:text>  <xsl:value-of select="."/> </xsl:template>     </xsl:stylesheet>

When you process greet.xml with greet.xsl using Xalan as shown:

xalan greet.xml greet.xsl

you get the following output:

French: Bienvenue

When this stylesheet processes greeting elements in the source tree, it tests whether the element has an xml:lang attribute with the specified value fr. When the pattern matches such an element, the lang( ) function returns true, and the template is instantiated.

Now, I'll move on to XPath node-set 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