XML Tutorial/XSLT stylesheet/mod — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
Do calculation in select: 11 mod 4
File: Data.xml
<?xml version="1.0"?>
<numbers>
<x>4</x>
<y>3.2</y>
<z>11</z>
</numbers>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:template match="numbers">
11 mod 4 =
<xsl:value-of select="z mod x" />
</xsl:template>
</xsl:stylesheet>
Output:
11 mod 4 =
3
Operator mod returns the remainder from a truncating division
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>
<number>11</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="/">
<Paragraph>
<xsl:value-of select="//number[5]"/>
<xsl:text> / </xsl:text>
<xsl:value-of select="//number[6]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[5] div //number[6]"/>
</Paragraph>
<Paragraph>
<xsl:value-of select="//number[5]"/>
<xsl:text> mod </xsl:text>
<xsl:value-of select="//number[6]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[5] mod //number[6]"/>
</Paragraph>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><Paragraph>8 / 11 = 0.7272727272727273</Paragraph><Paragraph>8 mod 11 = 8</Paragraph>
Tests of the mod operator in XPath 1.0
File: Data.xml
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>Tests of the mod operator in XPath 1.0
</xsl:text>
<xsl:text>
 9 mod 3 = </xsl:text>
<xsl:value-of select="9 mod 3"/>
<xsl:text>
 9 mod 3.8 = </xsl:text>
<xsl:value-of select="9 mod 3.8"/>
<xsl:text>
 9 mod "4" = </xsl:text>
<xsl:value-of select="9 mod "4""/>
<xsl:text>
 9 mod "Q" = </xsl:text>
<xsl:value-of select="9 mod "Q""/>
<xsl:text>
 9 mod true() = </xsl:text>
<xsl:value-of select="9 mod true()"/>
<xsl:text>
 9 mod false() = </xsl:text>
<xsl:value-of select="9 mod false()"/>
</xsl:template>
</xsl:stylesheet>
Output:
Tests of the mod operator in XPath 1.0
9 mod 3 = 0
9 mod 3.8 = 1.4000000000000004
9 mod "4" = 1
9 mod "Q" = NaN
9 mod true() = 0
9 mod false() = NaN
Tests of the mod operator in XPath 2.0
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>Tests of the mod operator in XPath 2.0
</xsl:text>
<xsl:text>
 9 mod 3 = </xsl:text>
<xsl:value-of select="9 mod 3"/>
<xsl:text>
 9 mod 3.8 = </xsl:text>
<xsl:value-of select="9 mod 3.8"/>
<xsl:text>
 9 mod number("4") = </xsl:text>
<xsl:value-of select="9 mod number("4")"/>
<xsl:text>
 9 mod number("Q") = </xsl:text>
<xsl:value-of select="9 mod number("Q")"/>
<xsl:text>
 9 mod number(true()) = </xsl:text>
<xsl:value-of select="9 mod number(true())"/>
<xsl:text>
 9 mod number(false()) = </xsl:text>
<xsl:value-of select="9 mod number(false())"/>
</xsl:template>
</xsl:stylesheet>
Output:
Tests of the mod operator in XPath 2.0
9 mod 3 = 0
9 mod 3.8 = 1.4
9 mod number("4") = 1
9 mod number("Q") = NaN
9 mod number(true()) = 0
9 mod number(false()) = NaN