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

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

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

Add string and integer together

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 + "hello" =
    <xsl:value-of select="z + "hello"" />
  </xsl:template>
</xsl:stylesheet>
Output:

    11 + "hello" =
    NaN


An example of the unary minus

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="x" as="xs:integer" select="xs:integer(-10)"/>
    <xsl:text>An example of the unary minus </xsl:text>
    <xsl:text>operator:&#xA;    </xsl:text>
    <xsl:value-of select="("$x =", $x, "&#xA;   -$x = ", -$x)"/>
  </xsl:template>
</xsl:stylesheet>
Output:
An example of the unary minus operator:
    $x = -10 
   -$x =  10


An example of the unary plus

File: Data.xml

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="x" as="xs:integer" select="xs:integer(-10)"/>
    <xsl:text>An example of the unary plus </xsl:text>
    <xsl:text>operator:&#xA;    </xsl:text>
    <xsl:value-of select="("$x =", $x, "&#xA;   +$x =", +$x)"/>
  </xsl:template>
</xsl:stylesheet>
Output:
An example of the unary plus operator:
    $x = -10 
   +$x = -10


Calculate with variable

File: Data.xml
<?xml version="1.0"?>
<catalog>
 <item id="0001">
  <maker>factory</maker>
  <description>device</description>
  <size>L</size>
  <price>120.00</price>
  <currency>USD</currency>
 </item>
</catalog>

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:output method="xml" indent="yes" />
  <xsl:variable name="discount" select="0.10" />
  <xsl:template match="catalog">
    <xsl:copy>
      <xsl:apply-templates select="item" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="item">
    <xsl:copy>
      <xsl:attribute name="id"><xsl:value-of select="@id" />
      </xsl:attribute>
      <xsl:copy-of select="maker|description|size|price" />
      <discount>
        <xsl:value-of select="$discount" />
      </discount>
      <discountPrice>
        <xsl:value-of select="price - (price * $discount)" />
      </discountPrice>
      <xsl:copy-of select="currency" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <item id="0001">
      <maker>factory</maker>
      <description>device</description>
      <size>L</size>
      <price>120.00</price>
      <discount>0.1</discount>
      <discountPrice>108</discountPrice>
      <currency>USD</currency>
   </item>
</catalog>


Do calculation in select: 11/3.2 (div)

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/3.2 =
    <xsl:value-of select="z div y" />
  </xsl:template>
</xsl:stylesheet>
Output:

    11/3.2 =
    3.4375


== Do calculation in select:/td>




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">
    3.2 - 4 =
    <xsl:value-of select="y - x" />
  </xsl:template>
</xsl:stylesheet>
Output:

    3.2 - 4 =
    -0.7999999999999998


Do calculation in select:2

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">
    4 + 3.2 =
    <xsl:value-of select="x + y" />
  </xsl:template>
</xsl:stylesheet>
Output:

    4 + 3.2 =
    7.2


Do calculation in select:2 * 11

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">
    4 + 3.2 * 11 =
    <xsl:value-of select="x+y*z" />
  </xsl:template>
</xsl:stylesheet>
Output:

    4 + 3.2 * 11 =
    39.2


Do calculation in select:2 (multiply)

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">
    4 * 3.2 =
    <xsl:value-of select="x * y" />
  </xsl:template>
</xsl:stylesheet>
Output:

    4 * 3.2 =
    12.8


Math calculation

File: Data.xml
<math>
     <operand>12</operand>
     <operand>23</operand>
     <operand>45</operand>
     <operand>56</operand>
     <operand>75</operand>
</math>
File: Transform.xslt
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="math">
    <xsl:apply-templates select="operand" />
  </xsl:template>
  <xsl:template match="operand">
    <xsl:value-of select="." />
    <xsl:text> + 25 = </xsl:text>
    <xsl:value-of select=". + 25" />
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="." />
    <xsl:text> * 25 = </xsl:text>
    <xsl:value-of select=". * 25" />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
12 + 25 = 37
12 * 25 = 300
23 + 25 = 48
23 * 25 = 575
45 + 25 = 70
45 * 25 = 1125
56 + 25 = 81
56 * 25 = 1400
75 + 25 = 100
75 * 25 = 1875


Output calculated value

File: Data.xml
<?xml version="1.0"?>
<catalog>
 <item id="0001">
  <maker>factory</maker>
  <description>device</description>
  <size>L</size>
  <price>120.00</price>
  <currency>USD</currency>
 </item>
</catalog>
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:output method="xml" indent="yes" />
  <xsl:variable name="discount" select="document("discount.xml")" />
  <xsl:template match="catalog">
    <xsl:copy>
      <xsl:apply-templates select="item" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="item">
    <xsl:copy>
      <xsl:attribute name="id"><xsl:value-of select="@id" />
      </xsl:attribute>
      <xsl:copy-of select="maker|description|size|price" />
      <discount>
        <xsl:value-of select="$discount" />
      </discount>
      <discountPrice>
        <xsl:value-of
          select="format-number(price - (price*$discount),"###.00")" />
      </discountPrice>
      <xsl:copy-of select="currency" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <item id="0001">
      <maker>factory</maker>
      <description>device</description>
      <size>L</size>
      <price>120.00</price>
      <discount/>
      <discountPrice>NaN</discountPrice>
      <currency>USD</currency>
   </item>
</catalog>


subtraction and multiplication uses common syntax

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[1]"/>
        <xsl:text> + </xsl:text>
        <xsl:value-of select="//number[2]"/>
        <xsl:text> = </xsl:text>
        <xsl:value-of select="//number[1] + //number[2]"/>
      </Paragraph>
      <Paragraph>
        <xsl:value-of select="//number[3]"/>
        <xsl:text> - </xsl:text>
        <xsl:value-of select="//number[4]"/>
        <xsl:text> = </xsl:text>
        <xsl:value-of select="//number[3] - //number[4]"/>
      </Paragraph>
      <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] * //number[6]"/>
      </Paragraph>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><Paragraph>1 + 3 = 4</Paragraph><Paragraph>4 - 17 = -13</Paragraph><Paragraph>8 * 11 = 88</Paragraph>


Tests of XPath multiplication in XPath 1.0

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Tests of XPath multiplication in XPath 1.0&#xA;</xsl:text>
    <xsl:text>&#xA;  9 * 3 = </xsl:text>
    <xsl:value-of select="9 * 3"/>
    <xsl:text>&#xA;  9 * 3.8 = </xsl:text>
    <xsl:value-of select="9 * 3.8"/>
    <xsl:text>&#xA;  9 * "4" = </xsl:text>
    <xsl:value-of select="9 * "4""/>
    <xsl:text>&#xA;  9 * "Q" = </xsl:text>
    <xsl:value-of select="9 * "Q""/>
    <xsl:text>&#xA;  9 * true() = </xsl:text>
    <xsl:value-of select="9 * true()"/>
    <xsl:text>&#xA;  9 * false() = </xsl:text>
    <xsl:value-of select="9 * false()"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Tests of XPath multiplication in XPath 1.0
  9 * 3 = 27
  9 * 3.8 = 34.199999999999996
  9 * "4" = 36
  9 * "Q" = NaN
  9 * true() = 9
  9 * false() = 0


Tests of XPath multiplication 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"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Tests of XPath multiplication in XPath 2.0&#xA;</xsl:text>
    <xsl:text>&#xA;  9 * 3 = </xsl:text>
    <xsl:value-of select="9 * 3"/>
    <xsl:text>&#xA;  9 * 3.8 = </xsl:text>
    <xsl:value-of select="9 * 3.8"/>
    <xsl:text>&#xA;  9 * number("4") = </xsl:text>
    <xsl:value-of select="9 * number("4")"/>
    <xsl:text>&#xA;  9 * number("Q") = </xsl:text>
    <xsl:value-of select="9 * number("Q")"/>
    <xsl:text>&#xA;  9 * number(true()) = </xsl:text>
    <xsl:value-of select="9 * number(true())"/>
    <xsl:text>&#xA;  9 * number(false()) = </xsl:text>
    <xsl:value-of select="9 * number(false())"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Tests of XPath multiplication in XPath 2.0
  9 * 3 = 27
  9 * 3.8 = 34.2
  9 * number("4") = 36
  9 * number("Q") = NaN
  9 * number(true()) = 9
  9 * number(false()) = 0


Tests of XPath subtraction in XPath 1.0

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Tests of XPath subtraction in XPath 1.0&#xA;</xsl:text>
    <xsl:text>&#xA;  9 - 3 = </xsl:text>
    <xsl:value-of select="9 - 3"/>
    <xsl:text>&#xA;  9 - 3.8 = </xsl:text>
    <xsl:value-of select="9 - 3.8"/>
    <xsl:text>&#xA;  9 - "4" = </xsl:text>
    <xsl:value-of select="9 - "4""/>
    <xsl:text>&#xA;  9 - "Q" = </xsl:text>
    <xsl:value-of select="9 - "Q""/>
    <xsl:text>&#xA;  9 - true() = </xsl:text>
    <xsl:value-of select="9 - true()"/>
    <xsl:text>&#xA;  9 - false() = </xsl:text>
    <xsl:value-of select="9 - false()"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Tests of XPath subtraction in XPath 1.0
  9 - 3 = 6
  9 - 3.8 = 5.2
  9 - "4" = 5
  9 - "Q" = NaN
  9 - true() = 8
  9 - false() = 9


Tests of XPath subtraction in XPath 2.0

File: Data.xml

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Tests of XPath subtraction in XPath 2.0&#xA;</xsl:text>
    <xsl:text>&#xA;  9 - 3 = </xsl:text>
    <xsl:value-of select="9 - 3"/>
    <xsl:text>&#xA;  9 - 3.8 = </xsl:text>
    <xsl:value-of select="9 - 3.8"/>
    <xsl:text>&#xA;  9 - number("4") = </xsl:text>
    <xsl:value-of select="9 - number("4")"/>
    <xsl:text>&#xA;  9 - number("Q") = </xsl:text>
    <xsl:value-of select="9 - number("Q")"/>
    <xsl:text>&#xA;  9 - number(true()) = </xsl:text>
    <xsl:value-of select="9 - number(true())"/>
    <xsl:text>&#xA;  9 - number(false()) = </xsl:text>
    <xsl:value-of select="9 - number(false())"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Tests of XPath subtraction in XPath 2.0
  9 - 3 = 6
  9 - 3.8 = 5.2
  9 - number("4") = 5
  9 - number("Q") = NaN
  9 - number(true()) = 8
  9 - number(false()) = 9