XML Tutorial/XSLT stylesheet/Boolean Functions — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11: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
<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="/">
node-set</TH>
<td>boolean</TH> |
node-set</TH><td>boolean</TH> |
text</TH>
<td>boolean</TH> |
text</TH><td>boolean</TH> |