XML Tutorial/XSLT stylesheet/number
Содержание
- 1 count="manufacturer|car" level="any" format="1. "
- 2 demonstrates the default behaviour of xsl:number element
- 3 number count="manufacturer|car" level="multiple" format="1.1. "
- 4 number format=" 01. "
- 5 number format="1. "
- 6 number format="I "
- 7 number format: roman
- 8 number format=" a. "
- 9 number level="any" count="section | element" format="1. "
- 10 number value="1000000" grouping-size="3" grouping-separator=","
- 11 number value="count(car)" format="01"
- 12 number value="position()" format="1. "
- 13 Setting the attribute level into multiple
- 14 The attribute starts with format identificator followed by separator characters.
- 15 The format is given with format attribute.
- 16 xsl:number inserts formated numbers into output
- 17 xsl:number value="position()" format="a# "
- 18 xsl:number value="position()" format="I... "
count="manufacturer|car" level="any" format="1. "
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<cars>
<manufacturer name="Chevrolet">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Malibu</car>
</manufacturer>
<manufacturer name="Ford">
<car>Pinto</car>
<car>Mustang</car>
<car>Taurus</car>
</manufacturer>
<manufacturer name="Volkswagen">
<car>Beetle</car>
<car>Jetta</car>
<car>Passat</car>
<car>Touraeg</car>
</manufacturer>
</cars>
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:template match="/">
<xsl:text>Automobile manufacturers and their cars
</xsl:text>
<xsl:for-each select="cars/manufacturer">
<xsl:number count="manufacturer|car" level="any" format="1. "/>
<xsl:value-of select="@name"/>
<xsl:text>
</xsl:text>
<xsl:for-each select="car">
<xsl:number count="manufacturer|car" level="any" format="1. "/>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
Automobile manufacturers and their cars
1. Chevrolet
2. Cavalier
3. Corvette
4. Impala
5. Malibu
6. Ford
7. Pinto
8. Mustang
9. Taurus
10. Volkswagen
11. Beetle
12. Jetta
13. Passat
14. Touraeg
demonstrates the default behaviour of xsl:number element
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/>
</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>1</TD><TD>First Chapter</TD></TR><TR><TD>2</TD><TD>
Second Chapter
</TD></TR><TR><TD>1</TD><TD>Subchapter 1</TD></TR><TR><TD>2</TD><TD>Subchapter 2</TD></TR><TR><TD>3</TD><TD>
Third Chapter
</TD></TR><TR><TD>1</TD><TD>Subchapter A</TD></TR><TR><TD>2</TD><TD>
Subchapter B
</TD></TR><TR><TD>1</TD><TD>sub a</TD></TR><TR><TD>2</TD><TD>sub b</TD></TR><TR><TD>3</TD><TD>Subchapter C</TD></TR></TABLE>
number count="manufacturer|car" level="multiple" format="1.1. "
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<cars>
<manufacturer name="Chevrolet">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Malibu</car>
</manufacturer>
<manufacturer name="Ford">
<car>Pinto</car>
<car>Mustang</car>
<car>Taurus</car>
</manufacturer>
<manufacturer name="Volkswagen">
<car>Beetle</car>
<car>Jetta</car>
<car>Passat</car>
<car>Touraeg</car>
</manufacturer>
</cars>
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:template match="/">
<xsl:text>Automobile manufacturers and their cars
</xsl:text>
<xsl:for-each select="cars/manufacturer">
<xsl:number count="manufacturer" format="1. "/>
<xsl:value-of select="@name"/>
<xsl:text>
</xsl:text>
<xsl:for-each select="car">
<xsl:number count="manufacturer|car" level="multiple" format="1.1. "/>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
Automobile manufacturers and their cars
1. Chevrolet
1.1. Cavalier
1.2. Corvette
1.3. Impala
1.4. Malibu
2. Ford
2.1. Pinto
2.2. Mustang
2.3. Taurus
3. Volkswagen
3.1. Beetle
3.2. Jetta
3.3. Passat
3.4. Touraeg
number format=" 01. "
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<provinces>
<name>Alberta</name>
<name>British Columbia</name>
<name>Manitoba</name>
<name>New Brunswick</name>
<name>Newfoundland and Labrador</name>
<name>Northwest Territories</name>
<name>Nova Scotia</name>
<name>Nunavut</name>
<name>Ontario</name>
<name>Prince Edward Island</name>
<name>Quebec</name>
<name>Saskatchewan</name>
<name>Yukon</name>
</provinces>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="provinces">
<xsl:apply-templates select="name" />
</xsl:template>
<xsl:template match="name">
<xsl:number format=" 01.	" />
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
01. Alberta
02. British Columbia
03. Manitoba
04. New Brunswick
05. Newfoundland and Labrador
06. Northwest Territories
07. Nova Scotia
08. Nunavut
09. Ontario
10. Prince Edward Island
11. Quebec
12. Saskatchewan
13. Yukon
number format="1. "
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<cars>
<manufacturer name="Chevrolet">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Malibu</car>
</manufacturer>
<manufacturer name="Ford">
<car>Pinto</car>
<car>Mustang</car>
<car>Taurus</car>
</manufacturer>
<manufacturer name="Volkswagen">
<car>Beetle</car>
<car>Jetta</car>
<car>Passat</car>
<car>Touraeg</car>
</manufacturer>
</cars>
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="html"/>
<xsl:template match="/">
<html>
<head>
<title>Automobile manufacturers and their cars</title>
</head>
<body>
<xsl:for-each select="cars/manufacturer">
<paragraph>
<xsl:number format="1. "/>
<xsl:value-of select="@name"/>
</paragraph>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Automobile manufacturers and their cars</title>
</head>
<body>
<paragraph>1. Chevrolet</paragraph>
<paragraph>2. Ford</paragraph>
<paragraph>3. Volkswagen</paragraph>
</body>
</html>
number format="I "
File: Data.xml
<?xml version="1.0"?>
<us>
<state>South Carolina</state>
<state>South Dakota</state>
<state>Tennessee</state>
<state>Texas</state>
<state>Utah</state>
<state>Vermont</state>
<state>Virginia</state>
<state>Washington</state>
<state>West Virginia</state>
<state>Wisconsin</state>
<state>Wyoming</state>
</us>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="us">
<xsl:apply-templates select="state" />
</xsl:template>
<xsl:template match="state">
<xsl:number format="I	" />
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
I South Carolina
II South Dakota
III Tennessee
IV Texas
V Utah
VI Vermont
VII Virginia
VIII Washington
IX West Virginia
X Wisconsin
XI Wyoming
number format: roman
File: Data.xml
<?xml version="1.0"?>
<us>
<state>Alabama</state>
<state>Alaska</state>
<state>Arizona</state>
<state>Arkansas</state>
<state>West Virginia</state>
<state>Wisconsin</state>
<state>Wyoming</state>
</us>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="us">
<xsl:apply-templates select="state" />
</xsl:template>
<xsl:template match="state">
<xsl:number format="i	" />
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
i Alabama
ii Alaska
iii Arizona
iv Arkansas
v West Virginia
vi Wisconsin
vii Wyoming
number format=" a. "
File: Data.xml
<?xml version="1.0"?>
<us>
<state>Alabama</state>
<state>Alaska</state>
<state>Arizona</state>
<state>Arkansas</state>
<state>California</state>
<state>BC</state>
<state>Connecticut</state>
<state>Delaware</state>
<state>Florida</state>
<state>Georgia</state>
<state>Hawaii</state>
<state>Wisconsin</state>
<state>Wyoming</state>
</us>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="us">
<xsl:apply-templates select="state" />
</xsl:template>
<xsl:template match="state">
<xsl:number format=" a.	" />
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
a. Alabama
b. Alaska
c. Arizona
d. Arkansas
e. California
f. BC
g. Connecticut
h. Delaware
i. Florida
j. Georgia
k. Hawaii
l. Wisconsin
m. Wyoming
number level="any" count="section | element" format="1. "
File: Data.xml
<?xml version="1.0"?>
<outline>
<section title="US coin denominations">
<element>cent</element>
<element>nickel</element>
<element>dime</element>
<element>quarter</element>
<element>half dollar</element>
<element>dollar</element>
</section>
</outline>
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:template match="outline">
<xsl:for-each select="section|//element">
<xsl:number level="any" count="section | element" format="1. " />
<xsl:value-of select="@title | text()" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
1. US coin denominations
2. cent
3. nickel
4. dime
5. quarter
6. half dollar
7. dollar
number value="1000000" grouping-size="3" grouping-separator=","
File: Data.xml
<thanks>Thanks a </thanks>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="thanks">
<xsl:value-of select="." />
<xsl:number value="1000000" grouping-size="3"
grouping-separator="," />
<xsl:text>!</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
Thanks a 1,000,000!
number value="count(car)" format="01"
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- cars.xml -->
<cars>
<manufacturer name="Chevrolet">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Malibu</car>
</manufacturer>
<manufacturer name="Ford">
<car>Pinto</car>
<car>Mustang</car>
<car>Taurus</car>
</manufacturer>
<manufacturer name="Volkswagen">
<car>Beetle</car>
<car>Jetta</car>
<car>Passat</car>
<car>Touraeg</car>
</manufacturer>
</cars>
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="html"/>
<xsl:template match="/">
<html>
<head>
<title>Automobile manufacturers and their cars</title>
</head>
<body>
<xsl:for-each select="cars/manufacturer">
<paragraph>
<xsl:text>Cars produced by </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>: </xsl:text>
<xsl:number value="count(car)" format="01"/>
</paragraph>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Automobile manufacturers and their cars</title>
</head>
<body>
<paragraph>Cars produced by Chevrolet: 04</paragraph>
<paragraph>Cars produced by Ford: 03</paragraph>
<paragraph>Cars produced by Volkswagen: 04</paragraph>
</body>
</html>
number value="position()" format="1. "
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<provinces>
<name>Alberta</name>
<name>British Columbia</name>
<name>Manitoba</name>
<name>New Brunswick</name>
<name>Newfoundland and Labrador</name>
<name>Northwest Territories</name>
<name>Nova Scotia</name>
<name>Nunavut</name>
<name>Ontario</name>
<name>Prince Edward Island</name>
<name>Quebec</name>
<name>Saskatchewan</name>
<name>Yukon</name>
</provinces>
File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="provinces">
<xsl:apply-templates select="name">
<xsl:sort order="descending" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="name">
<xsl:number value="position()" format="1. " />
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
1. Yukon
2. Saskatchewan
3. Quebec
4. Prince Edward Island
5. Ontario
6. Nunavut
7. Nova Scotia
8. Northwest Territories
9. Newfoundland and Labrador
10. New Brunswick
11. Manitoba
12. British Columbia
13. Alberta
Setting the attribute level into multiple
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"/>
</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>1</TD><TD>First Chapter</TD></TR><TR><TD>2</TD><TD>
Second Chapter
</TD></TR><TR><TD>2.1</TD><TD>Subchapter 1</TD></TR><TR><TD>2.2</TD><TD>Subchapter 2</TD></TR><TR><TD>3</TD><TD>
Third Chapter
</TD></TR><TR><TD>3.1</TD><TD>Subchapter A</TD></TR><TR><TD>3.2</TD><TD>
Subchapter B
</TD></TR><TR><TD>3.2.1</TD><TD>sub a</TD></TR><TR><TD>3.2.2</TD><TD>sub b</TD></TR><TR><TD>3.3</TD><TD>Subchapter C</TD></TR></TABLE>
The attribute starts with format identificator followed by separator characters.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<n>one</n>
<n>two</n>
<n>three</n>
<n>four</n>
</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>
<xsl:for-each select="//n">
<TR>
<TD>
<xsl:number value="position()" format="A "/>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>A one</TD></TR><TR><TD>B two</TD></TR><TR><TD>C three</TD></TR><TR><TD>D four</TD></TR></TABLE>
The format is given with format attribute.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<n>one</n>
<n>two</n>
<n>three</n>
<n>four</n>
</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>
<xsl:for-each select="//n">
<TR>
<TD>
<xsl:number value="position()" format="001. "/>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template></xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>001. one</TD></TR><TR><TD>002. two</TD></TR><TR><TD>003. three</TD></TR><TR><TD>004. four</TD></TR></TABLE>
xsl:number inserts formated numbers into output
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<n>one</n>
<n>two</n>
<n>three</n>
<n>four</n>
</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>
<xsl:for-each select="//n">
<TR>
<TD>
<xsl:number value="position()" format="1. "/>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>1. one</TD></TR><TR><TD>2. two</TD></TR><TR><TD>3. three</TD></TR><TR><TD>4. four</TD></TR></TABLE>
xsl:number value="position()" format="a# "
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<n>one</n>
<n>two</n>
<n>three</n>
<n>four</n>
</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>
<xsl:for-each select="//n">
<TR>
<TD>
<xsl:number value="position()" format="a# "/>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>a# one</TD></TR><TR><TD>b# two</TD></TR><TR><TD>c# three</TD></TR><TR><TD>d# four</TD></TR></TABLE>
xsl:number value="position()" format="I... "
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<n>one</n>
<n>two</n>
<n>three</n>
<n>four</n>
</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>
<xsl:for-each select="//n">
<TR>
<TD>
<xsl:number value="position()" format="I... "/>
<xsl:value-of select="."/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE><TR><TD>I... one</TD></TR><TR><TD>II... two</TD></TR><TR><TD>III... three</TD></TR><TR><TD>IV... four</TD></TR></TABLE>