The XPath Boolean Functions

The XPath Boolean Functions

XPath also supports the following set of Boolean functions:

  • boolean() . Converts its argument to a Boolean value.

  • false() . Returns a value of false.

  • lang() . Tests whether the language set with the xml:lang attribute is the same as the language passed to this function.

  • not() . Reverses the true.false value of its argument.

  • true() . Returns a value of true.

boolean()

The boolean function converts its argument to a Boolean value. Heres how you use this function:

 boolean boolean(object) 

Heres what happens when you pass this function arguments of various XPath types:

  • number . If the number is zero, the result is false; otherwise the result is true. NaN always returns false.

  • string . If the string is not empty, the result is true, otherwise the result is false.

  • boolean . Value not changed.

  • node set . Empty node set is false, true otherwise.

  • Result tree fragment in XSLT 1.0 always true.

false()

The false function returns a value of false. Heres how you use it:

 boolean false() 

XPath does not define any Boolean constants, so if you need to assign a value of false to a variable, you can use the false function. (Youll see variables in Chapter 9.)

lang()

The lang function tests whether the language of the context node (as defined by the xml:lang attribute) is the same as the language you pass to it. Heres how you use this function:

 boolean lang(string) 

You pass this function a string that corresponds to a language in the XML specification, such as en for English, de for German, and jp for Japanese.

In the following example Im checking to make sure that the source document was written in English. I start by setting the xml:lang attribute in planets.xml to English:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS xml:lang="en">      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

Now I test to make sure that the source documents language is indeed English before applying templates, as you see here:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <xsl:if test="lang('en')">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>          </xsl:if>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>          .          .          .  </xsl:stylesheet> 

not()

The not function reverses the logical sense of its argument. If you pass it an argument whose logical value is true, this function returns false, and if you pass it a false argument, it returns true. Heres how you use this function:

 boolean not(boolean) 

In the following example, which you saw in Chapter 4, I want to select only elements that have both COLOR and POPULATED attributes. To do that, I use the match predicate [@COLOR and @POPULATED]. To strip out the other elements so the default rules dont place their text into the result document, I use the predicate [not(@COLOR) or not(@POPULATED)]:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>          .          .          .          </HTML>      </xsl:template>      <xsl:template match="PLANET[@COLOR and @POPULATED]">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>      </xsl:template>      <xsl:template match="PLANET[not(@COLOR) or not(@POPULATED)]">      </xsl:template>      <xsl:template match="MASS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

And heres the result:

 <HTML>      <HEAD>          <TITLE>              Colorful, Populated Planets          </TITLE>      </HEAD>      <BODY>          <H1>              Colorful, Populated Planets          </H1>          <TABLE BORDER="2">              <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>              </TR>              <TR>                  <TD>Earth</TD>                  <TD>1 (Earth = 1)</TD>                  <TD>2107 miles</TD>                  <TD>1 days</TD              </TR>          </TABLE>      </BODY>  </HTML> 

true()

The true function returns a value of true. Heres how you use it:

 boolean true() 

XPath does not define any Boolean constants, so if you need to assign a value of true to a variable, you can use the true function. (Youll see variables in Chapter 9.)



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

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