XML Tutorial/XSLT stylesheet/Boolean Functions — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:26, 26 мая 2010

Boolean Functions

   <source lang="xml">

boolean() takes an object as its argument and returns a Boolean value.

   If the argument is a number, true is returned if the number is not zero or NaN. 
   If the argument is a node-set, true is returned if the node-set is not empty. 
   If the argument is a string, true is returned if the string is not empty. 

false() takes no argument and returns the Boolean value false lang() takes a string argument.

      Returns true if the language of the node is the language indicated by the string argument or one of its sublanguages. 

not() takes a Boolean expression as its argument true() has no argument and returns the Boolean value true</source>


compares "0" as a string and as a number

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <text>999</text>
   <text>A123</text>
   <text>-16</text>
   <text>0</text>
   <text/>
   <text>false</text>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
     <paragraph>
       <xsl:text>The boolean value of "0" is </xsl:text>
       
         <xsl:value-of select="boolean(//text[text()="0"])"/>
       
       <xsl:text> if "0" is a string, but </xsl:text>
       
         <xsl:value-of select="boolean(number((//text[text()="0"])))"/>
       
       <xsl:text> if "0" is a number.</xsl:text>
     </P>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><paragraph>The boolean value of "0" is true if "0" is a string, but false if "0" is a number.</P></source>


Functions true() and false() are useful, when some conditions are tested during programming

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <number>0</number>
   <number>1</number>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="number">
     <paragraph>
       <xsl:if test="true()">
         <xsl:text>true </xsl:text>
       </xsl:if>
       <xsl:if test="not(false())">
         <xsl:text>not false</xsl:text>
       </xsl:if>
     </P>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

   <paragraph>true not false</P>
   <paragraph>true not false</P></source>
   
  

The not function returns true if its argument is false, and false otherwise.

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <car id="a234" checked="yes"/>
   <car id="a111" checked="yes"/>
   <car id="a005"/>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="car[not(@checked)]">
     <paragraph>
       
         <xsl:value-of select="@id"/>
       
     </P>
   </xsl:template>
   <xsl:template match="car[@checked]">
     <paragraph>
       
         <xsl:value-of select="@id"/>
       
     </P>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

   <paragraph>a234</P>
   <paragraph>a111</P>
   <paragraph>a005</P></source>
   
  

uses node-sets as arguments for boolean() function

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <text>999</text>
   <text>A123</text>
   <text>-16</text>
   <text>0</text>
   <text/>
   <text>false</text>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
<TR> <TD> <xsl:text>/</xsl:text> </TD> <TD> <xsl:value-of select="boolean(/)"/> </TD> </TR> <TR> <TD> <xsl:text>//text</xsl:text> </TD> <TD> <xsl:value-of select="boolean(//text)"/> </TD> </TR> <TR> <TD> <xsl:text>//number</xsl:text> </TD> <TD> <xsl:value-of select="boolean(//number)"/> </TD> </TR> <TR> <TD> <xsl:text>//text[23]</xsl:text> </TD> <TD> <xsl:value-of select="boolean(//text[23])"/> </TD> </TR> </TABLE> </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
node-set</TH>
         <td>boolean</TH>
<TR><TD>/</TD><TD>true</TD></TR><TR><TD>//text</TD><TD>true</TD></TR><TR><TD>//number</TD><TD>false</TD></TR><TR><TD>//text[23]</TD><TD>false</TD></TR></TABLE></source>


Use strings as arguments of boolean() function.

   <source lang="xml">

A string is true if and only if its length is non-zero.

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

   <text>999</text>
   <text>A123</text>
   <text>-16</text>
   <text>0</text>
   <text/>
   <text>false</text>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet

     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
node-set</TH><td>boolean</TH>
<xsl:for-each select="//text"> <TR> <TD> <xsl:value-of select="."/> <xsl:text/> </TD> <TD> <xsl:value-of select="boolean(text())"/> </TD> </TR> </xsl:for-each> </TABLE> </xsl:template> </xsl:stylesheet> <?xml version="1.0" encoding="UTF-8"?>
text</TH>
         <td>boolean</TH>
<TR><TD>999</TD><TD>true</TD>

</TR><TR><TD>A123</TD><TD>true</TD></TR><TR><TD>-16</TD><TD>true </TD></TR><TR><TD>0</TD><TD>true</TD></TR><TR><TD/><TD>false</TD> </TR><TR><TD>false</TD><TD>true</TD></TR></TABLE></source>

text</TH><td>boolean</TH>