XML Tutorial/XSLT stylesheet/choose — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
Change style for even and odd
File: Data.xml
<?xml version="1.0"?>
<!-- albums.xml -->
<list xml:lang="en">
<title>title 1</title>
<listitem>item 1</listitem>
<listitem>item 2</listitem>
<listitem>item 3</listitem>
<listitem xml:lang="sw">item 4</listitem>
<listitem xml:lang="en-gb">item 5</listitem>
<listitem xml:lang="zu">item 6</listitem>
<listitem xml:lang="jz">item 7</listitem>
</list>
File: Transform.xslt
<?xml version="1.0"?>
<!-- comment.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>XSLT and CSS Demo</title>
<style>
<xsl:comment>
p.big {font-size: 125%; font-weight: bold;}
p.odd {color: purple; font-weight: bold;}
p.even {color: blue; font-style: italic; font-weight: bold;}
</xsl:comment>
</style>
</head>
<body style="font-family: sans-serif;">
<xsl:apply-templates select="list/title"/>
<xsl:apply-templates select="list/listitem"/>
</body>
</html>
</xsl:template>
<xsl:template match="title">
<p class="big"><xsl:value-of select="."/></p>
</xsl:template>
<xsl:template match="listitem">
<xsl:choose>
<xsl:when test="position() mod 2">
<p class="odd"><xsl:value-of select="."/></p>
</xsl:when>
<xsl:otherwise>
<p class="even"><xsl:value-of select="."/></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XSLT and CSS Demo</title><style>
<!--
p.big {font-size: 125%; font-weight: bold;}
p.odd {color: purple; font-weight: bold;}
p.even {color: blue; font-style: italic; font-weight: bold;}
--></style></head>
<body style="font-family: sans-serif;">
<p class="big">title 1</p>
<p class="odd">item 1</p>
<p class="even">item 2</p>
<p class="odd">item 3</p>
<p class="even">item 4</p>
<p class="odd">item 5</p>
<p class="even">item 6</p>
<p class="odd">item 7</p>
</body>
</html>
choose statement
File: Data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<america>
<source>
<title>F</title>
<url>http://www.wbex.ru</url>
<populations estimate="true" year="2002"/>
<nation> <name>USA</name> <capital>DC</capital> <population>32277942</population> <cc>dz</cc> </nation>
</america> 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="america"> <xsl:apply-templates select="nation" /> </xsl:template> <xsl:template match="nation"> <xsl:choose> <xsl:when test="population > 10000000"> <xsl:value-of select="name" /> <xsl:text> </xsl:text> </xsl:when> </xsl:choose> </xsl:template>
</xsl:stylesheet> Output: USA</source>
choose with otherwise statement
File: Data.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<america>
<source>
<title>A</title>
<url>http://www.wbex.ru</url>
<populations estimate="true" year="2002"/>
<nation> <name>USA</name> <capital>DC</capital> <population>32277942</population> <cc>dz</cc> </nation>
</america>
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="america"> <xsl:apply-templates select="nation" /> </xsl:template> <xsl:template match="nation"> <xsl:choose> <xsl:when test="population = 10000000"> <xsl:value-of select="name" /> <xsl:text> = 10M</xsl:text> <xsl:text> </xsl:text> </xsl:when> <xsl:when test="population = 1000000"> <xsl:value-of select="name" /> <xsl:text> = 1M</xsl:text> <xsl:text> </xsl:text> </xsl:when> <xsl:otherwise> <xsl:message terminate="yes">Not found!</xsl:message> </xsl:otherwise> </xsl:choose> </xsl:template>
</xsl:stylesheet></source>
when test="position() mod
File: Data.xml
<?xml version="1.0"?>
<!-- albums.xml -->
<list xml:lang="en">
<title>title 1</title>
<listitem>item 1</listitem>
<listitem>item 2</listitem>
<listitem>item 3</listitem>
<listitem xml:lang="sw">item 4</listitem>
<listitem xml:lang="en-gb">item 5</listitem>
<listitem xml:lang="zu">item 6</listitem>
<listitem xml:lang="jz">item 7</listitem>
</list>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select="list/title"/>
</title>
</head>
<body style="font-family: sans-serif; color: white;">
<h1 style="color: black;">
<xsl:value-of select="list/title"/>
</h1>
<table border="1" cellpadding="5"
style="font-weight: bold;">
<xsl:for-each select="list/listitem">
<tr>
<td>
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="position() mod 4 = 0">
<xsl:text>background: yellow; color: black;</xsl:text>
</xsl:when>
<xsl:when test="position() mod 4 = 1">
<xsl:text>background: blue;</xsl:text>
</xsl:when>
<xsl:when test="position() mod 4 = 2">
<xsl:text>background: white; color: black;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>background: black;</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>title 1</title>
</head>
<body style="font-family: sans-serif; color: white;">
<h1 style="color: black;">title 1</h1>
<table border="1" cellpadding="5" style="font-weight: bold;">
<tr>
<td style="background: blue;">item 1</td>
</tr>
<tr>
<td style="background: white; color: black;">item 2</td>
</tr>
<tr>
<td style="background: black;">item 3</td>
</tr>
<tr>
<td style="background: yellow; color: black;">item 4</td>
</tr>
<tr>
<td style="background: blue;">item 5</td>
</tr>
<tr>
<td style="background: white; color: black;">item 6</td>
</tr>
<tr>
<td style="background: black;">item 7</td>
</tr>
</table>
</body>
</html>
xsl:choose: check the value of an attribute
File: Data.xml
<?xml version = "1.0"?>
<planner>
<year value = "2000">
<date month = "7" day = "15">
<note time = "1430">meeting</note>
<note time = "1620">course</note>
</date>
<date month = "7" day = "4">
<note>Independence Day</note>
</date>
<date month = "7" day = "20">
<note time = "0900">Meeting A</note>
</date>
<date month = "7" day = "20">
<note time = "1900">Meeting B</note>
</date>
<date month = "7" day = "20">
<note time = "1300">Meeting C</note>
</date>
</year>
</planner>
File: Transform.xslt
<?xml version = "1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
Appointments
<br />
<xsl:apply-templates select="planner/year" />
</body>
</html>
</xsl:template>
<xsl:template match="year">
<strong>Year:</strong>
<xsl:value-of select="@value" />
<br />
<xsl:for-each select="date/note">
<xsl:sort select="../@day" order="ascending"
data-type="number" />
<strong>
Day:
<xsl:value-of select="../@day" />
/
<xsl:value-of select="../@month" />
</strong>
<br />
<xsl:choose>
<xsl:when
test="@time > "0500" and @time < "1200"">
Morning (
<xsl:value-of select="@time" />
):
</xsl:when>
<xsl:when
test="@time > "1200" and @time < "1700"">
Afternoon (
<xsl:value-of select="@time" />
):
</xsl:when>
<xsl:when
test="@time > "1200" and @time < "1700"">
Evening (
<xsl:value-of select="@time" />
):
</xsl:when>
<xsl:when
test="@time > "1200" and @time < "1700"">
Night (
<xsl:value-of select="@time" />
):
</xsl:when>
<xsl:otherwise>Entire day:</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." />
<xsl:if test=". = """>n/a</xsl:if>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
Appointments
<br><strong>Year:</strong>2000<br><strong>
Day:
4
/
7</strong><br>Entire day:Independence Day<br><strong>
Day:
15
/
7</strong><br>
Afternoon (
1430
):
Doctor"s appointment<br><strong>
Day:
15
/
7</strong><br>
Afternoon (
1620
):
course<br><strong>
Day:
20
/
7</strong><br>
Morning (
0900
):
Meeting A<br><strong>
Day:
20
/
7</strong><br>Entire day:Party at Joe"s<br><strong>
Day:
20
/
7</strong><br>
Afternoon (
1300
):
Meeting C<br></body>
</html>
xsl:choose element is used for selection between several possibilities
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<SECTION>
<DATA>I need a pen.</DATA>
<DATA>I need some paper.</DATA>
<SUMMARY>I need a pen and some paper</SUMMARY>
</SECTION>
<SECTION>
<DATA>I need bread.</DATA>
<DATA>I need butter.</DATA>
</SECTION>
</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="//SECTION">
<xsl:choose>
<xsl:when test="SUMMARY">
<paragraph>
<xsl:text>SUMMARY: </xsl:text>
<xsl:value-of select="SUMMARY"/>
</P>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="DATA">
<paragraph>
<xsl:text>DATA: </xsl:text>
<xsl:value-of select="."/>
</P>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph>SUMMARY: I need a pen and some paper</P>
<paragraph>DATA: I need bread.</P><paragraph>DATA: I need butter.</P>
xsl:choose, xsl:when and xsl:otherwise
File: Data.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<planner>
<year value="2002">
<date month="7" day="15">
<note time="1430">meeting</note>
<note time="1620">course</note>
</date>
<date month="7" day="4">
<note>Independence Day</note>
</date>
<date month="7" day="9">
<note />
</date>
</year>
</planner>
File: Transform.xslt
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns = "http://www.w3.org/1999/xhtml">
<xsl:output method = "xml" omit-xml-declaration = "no"
doctype-system =
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype-public =
"-//W3C//DTD XHTML 1.0 Strict//EN" />
<xsl:template match = "/">
<html>
<head><title>Conditional Processing</title></head>
<body>
<paragraph>Appointments
<br />
<xsl:apply-templates select = "planner/year" />
</p>
</body>
</html>
</xsl:template>
<xsl:template match = "year">
<strong>Year:</strong>
<xsl:value-of select = "@value" />
<br />
<xsl:for-each select = "date/note">
<!-- sort by date"s day attribute value -->
<xsl:sort select = "../@day" order = "ascending"
data-type = "number" />
<br />
<strong>
Day:
<xsl:value-of select = "../@month"/>/
<xsl:value-of select = "../@day"/>
</strong>
<br />
<xsl:choose>
<xsl:when test =
"@time > "0500" and @time < "1200"">
Morning (<xsl:value-of select = "@time" />):
</xsl:when>
<xsl:when test =
"@time > "1200" and @time < "1700"">
Afternoon (<xsl:value-of select = "@time" />):
</xsl:when>
<xsl:when test =
"@time > "1200" and @time < "2200"">
Evening (<xsl:value-of select = "@time" />):
</xsl:when>
<xsl:when test =
"@time > "2200" and @time < "500"">
Night (<xsl:value-of select = "@time" />):
</xsl:when>
<xsl:otherwise>
Entire day:
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select = "." />
<xsl:if test = ". = """>
n/a
</xsl:if>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Conditional Processing</title></head><body><paragraph>Appointments
<br/><strong>Year:</strong>2002<br/><br/><strong>
Day:
7/
4</strong><br/>
Entire day:
Independence Day<br/><br/><strong>
Day:
7/
9</strong><br/>
Entire day:
n/a
<br/><br/><strong>
Day:
7/
15</strong><br/>
Afternoon (1430):
Doctor"s appointment<br/><br/><strong>
Day:
7/
15</strong><br/>
Afternoon (1620):
course<br/></p></body></html>