XML Tutorial/XSLT stylesheet/number format
Содержание
- 1 format-number(123456.9999, "#,###.##", "us_default") : 123,457
- 2 format-number(1528.3, "#/###:00", "f1")
- 3 format-number(1 div 0, "###,###.00", "f2")
- 4 format-number(42, "#!","hash_mark")
- 5 format-number(999999999.9999, "#.###,##")
- 6 format number after calculation
- 7 format-number(blue div orange, "#.##", "f2")
- 8 format-number(number(1) div 0, "#.#")
- 9 format-number(number(1) div 0, "#.#", "other_options")
- 10 format-number(number("blue") * number("orange"), "#")
- 11 format-number(number("blue") * number("orange"), "#", "other_options")
- 12 formatting of multilevel numbers
- 13 select=format-number(catalog/item/price * 0.60, ###.00)
- 14 select=format-number($totalSales, ##,###)
format-number(123456.9999, "#,###.##", "us_default") : 123,457
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<!-- decimal-format.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(123456.9999, "#,###.##", "us_default") : </xsl:text>
<xsl:value-of select="format-number(123456.9999, "#,###.##", "us_default")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(123456.9999, "#,###.##", "us_default") : 123,457
format-number(1528.3, "#/###:00", "f1")
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:decimal-format name="f1" decimal-separator=":" grouping-separator="/"/>
<xsl:decimal-format name="f2" infinity="Really, really big" NaN="[not a number]"/>
<xsl:template match="/">
<xsl:text>format-number(1528.3, "#/###:00", "f1")=</xsl:text>
<xsl:value-of select="format-number(1528.3, "#/###:00;-#/###:00", "f1")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(1528.3, "#/###:00", "f1")=1/528:30
format-number(1 div 0, "###,###.00", "f2")
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:decimal-format name="f1" decimal-separator=":" grouping-separator="/"/>
<xsl:decimal-format name="f2" infinity="Really, really big" NaN="[not a number]"/>
<xsl:template match="/">
<xsl:text>format-number(1 div 0, "###,###.00", "f2")=</xsl:text>
<xsl:value-of select="format-number(1 div 0, "###,###.00", "f2")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(1 div 0, "###,###.00", "f2")=Really, really big
format-number(42, "#!","hash_mark")
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="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(42, "#!","hash_mark") : </xsl:text>
<xsl:value-of select="format-number(42, "#!", "hash_mark")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(42, "#!","hash_mark") : #42
format-number(999999999.9999, "#.###,##")
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<!-- decimal-format.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(999999999.9999, "#.###,##") : </xsl:text>
<xsl:value-of select="format-number(999999999.9999, "#.###,##")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(999999999.9999, "#.###,##") : 1.000.000.000
format number after calculation
File: Data.xml
<?xml version="1.0"?>
<catalog>
<item id="SC-0001">
<maker>A</maker>
<description>B</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:output doctype-system="catalog.dtd" />
<xsl:variable name="discount">0.70</xsl:variable>
<xsl:variable name="discountPrice"
select="format-number(catalog/item/price - (catalog/item/price) * $discount, "###.00")" />
<xsl:template match="catalog">
<xsl:copy>
<xsl:apply-templates select="item" />
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<xsl:copy>
<xsl:copy-of select="@id" />
<xsl:copy-of select="maker|description|size|price" />
<discount>
<xsl:value-of select="$discount" />
</discount>
<discountPrice>
<xsl:value-of select="$discountPrice" />
</discountPrice>
<xsl:copy-of select="currency" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog
SYSTEM "catalog.dtd">
<catalog>
<item id="SC-0001">
<maker>A</maker>
<description>B</description>
<size>L</size>
<price>120.00</price>
<discount>0.70</discount>
<discountPrice>36.00</discountPrice>
<currency>USD</currency>
</item>
</catalog>
format-number(blue div orange, "#.##", "f2")
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:decimal-format name="f1" decimal-separator=":" grouping-separator="/"/>
<xsl:decimal-format name="f2" infinity="Really, really big" NaN="[not a number]"/>
<xsl:template match="/">
<xsl:text>format-number(blue div orange, "#.##", "f2")=</xsl:text>
<xsl:value-of select="format-number(blue div orange, "#.##", "f2")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(blue div orange, "#.##", "f2")=[not a number]
format-number(number(1) div 0, "#.#")
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="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(number(1) div 0, "#.#") : </xsl:text>
<xsl:value-of select="format-number(number(1) div 0, "#.#")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(number(1) div 0, "#.#") : Infinity
format-number(number(1) div 0, "#.#", "other_options")
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<!-- decimal-format.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(number(1) div 0, "#.#", "other_options") : </xsl:text>
<xsl:value-of select="format-number(number(1) div 0, "#.#", "other_options")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(number(1) div 0, "#.#", "other_options") : unfathomably huge
format-number(number("blue") * number("orange"), "#")
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="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(number("blue") * number("orange"), "#") : </xsl:text>
<xsl:value-of select="format-number(number("blue") * number("orange"), "#")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(number("blue") * number("orange"), "#") : NaN
format-number(number("blue") * number("orange"), "#", "other_options")
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="text"/>
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:decimal-format name="us_default"/>
<xsl:decimal-format name="other_options" NaN="[not a number]" infinity="unfathomably huge"/>
<xsl:decimal-format name="hash_mark" digit="!"/>
<xsl:template match="/">
<xsl:text>format-number(number("blue") * number("orange"), "#", "other_options") : </xsl:text>
<xsl:value-of select="format-number(number("blue") * number("orange"), "#", "other_options")"/>
</xsl:template>
</xsl:stylesheet>
Output:
format-number(number("blue") * number("orange"), "#", "other_options") : [not a number]
formatting of multilevel numbers
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<chapter>First Chapter</chapter>
<chapter>
Second Chapter
<chapter>Subchapter 1</chapter>
<chapter>Subchapter 2</chapter>
</chapter>
<chapter>
Third Chapter
<chapter>Subchapter A</chapter>
<chapter>
Subchapter B
<chapter>sub a</chapter>
<chapter>sub b</chapter>
</chapter>
<chapter>Subchapter C</chapter>
</chapter>
</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>Number</TH>
<td>text</TH>
</TR>
<xsl:for-each select="//chapter">
<TR>
<TD>
<xsl:number level="multiple" format="I-1-a:"/>
</TD>
<TD>
<xsl:value-of select="./text()"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE BORDER="1"><TR><td>Number</TH><td>text</TH></TR><TR><TD>I:</TD><TD>First Chapter</TD></TR><TR><TD>II:</TD><TD>
Second Chapter
</TD></TR><TR><TD>II-1:</TD><TD>Subchapter 1</TD></TR><TR><TD>II-2:</TD><TD>Subchapter 2</TD></TR><TR><TD>III:</TD><TD>
Third Chapter
</TD></TR><TR><TD>III-1:</TD><TD>Subchapter A</TD></TR><TR><TD>III-2:</TD><TD>
Subchapter B
</TD></TR><TR><TD>III-2-a:</TD><TD>sub a</TD></TR><TR><TD>III-2-b:</TD><TD>sub b</TD></TR><TR><TD>III-3:</TD><TD>Subchapter C</TD></TR></TABLE>
select=format-number(catalog/item/price * 0.60, ###.00)
File: Data.xml
<?xml version="1.0"?>
<catalog>
<item id="SC-0001">
<maker>A</maker>
<description>B</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:output doctype-system="catalog.dtd" />
<xsl:variable name="discount">
<discount>0.40</discount>
<discountPrice>
<xsl:value-of
select="format-number(catalog/item/price * 0.60, "###.00")" />
</discountPrice>
</xsl:variable>
<xsl:template match="catalog">
<xsl:copy>
<xsl:apply-templates select="item" />
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<xsl:copy>
<xsl:copy-of select="@id" />
<xsl:copy-of select="maker|description|size|price" />
<xsl:copy-of select="$discount" />
<xsl:copy-of select="currency" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog
SYSTEM "catalog.dtd">
<catalog>
<item id="SC-0001">
<maker>A</maker>
<description>B</description>
<size>L</size>
<price>120.00</price>
<discount>0.40</discount>
<discountPrice>72.00</discountPrice>
<currency>USD</currency>
</item>
</catalog>
select=format-number($totalSales, ##,###)
File: Data.xml
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:decimal-format name="f1" decimal-separator=":" grouping-separator="/"/>
<xsl:decimal-format name="f2" infinity="Really, really big" NaN="[not a number]"/>
<xsl:template match="/">
<xsl:variable name="totalSales" select="sum(/report/brand/units)"/>
<xsl:for-each select="report/brand">
<xsl:value-of select="name"/>
<xsl:text>: 
 </xsl:text>
<xsl:value-of select="format-number(units, "##,###")"/>
<xsl:text> bars sold, </xsl:text>
<xsl:value-of select="format-number(units div $totalSales, "##%")"/>
<xsl:text> of all sales.</xsl:text>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:text>Total sales: </xsl:text>
<xsl:value-of select="format-number($totalSales, "##,###")"/>
<xsl:text> bars.
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
Total sales: bars.