XML Tutorial/XSLT stylesheet/Boolean Functions — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
- 1 Boolean Functions
- 2 compares "0" as a string and as a number
- 3 Functions true() and false() are useful, when some conditions are tested during programming
- 4 The not function returns true if its argument is false, and false otherwise.
- 5 uses node-sets as arguments for boolean() function
- 6 Use strings as arguments of boolean() function.
Boolean Functions
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
compares "0" as a string and as a number
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<text>999</text>
<text>A123</text>
<text>-16</text>
<text>0</text>
<text/>
<text>false</text>
</data>
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>
<B>
<xsl:value-of select="boolean(//text[text()="0"])"/>
</B>
<xsl:text> if "0" is a string, but </xsl:text>
<B>
<xsl:value-of select="boolean(number((//text[text()="0"])))"/>
</B>
<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 <B>true</B> if "0" is a string, but <B>false</B> if "0" is a number.</P>
Functions true() and false() are useful, when some conditions are tested during programming
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<number>0</number>
<number>1</number>
</data>
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>
The not function returns true if its argument is false, and false otherwise.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<car id="a234" checked="yes"/>
<car id="a111" checked="yes"/>
<car id="a005"/>
</data>
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>
<B style="color:red">
<xsl:value-of select="@id"/>
</B>
</P>
</xsl:template>
<xsl:template match="car[@checked]">
<paragraph>
<B style="color:blue">
<xsl:value-of select="@id"/>
</B>
</P>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph><B style="color:blue">a234</B></P>
<paragraph><B style="color:blue">a111</B></P>
<paragraph><B style="color:red">a005</B></P>
uses node-sets as arguments for boolean() function
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<text>999</text>
<text>A123</text>
<text>-16</text>
<text>0</text>
<text/>
<text>false</text>
</data>
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="/">
<TABLE border="1">
<TR>
<td>node-set</TH>
<td>boolean</TH>
</TR>
<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"?><TABLE border="1"><TR><td>node-set</TH><td>boolean</TH></TR><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>
Use strings as arguments of boolean() function.
A string is true if and only if its length is non-zero.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<text>999</text>
<text>A123</text>
<text>-16</text>
<text>0</text>
<text/>
<text>false</text>
</data>
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="/">
<TABLE border="1">
<TR>
<td>text</TH>
<td>boolean</TH>
</TR>
<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"?><TABLE border="1"><TR>
<td>text</TH><td>boolean</TH></TR><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>