XML Tutorial/XSLT stylesheet/table — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
Format table cell with choose statement
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <list xml:lang="en">
<title>title 1</title> <listelement>element 1</listelement> <listelement>element 2</listelement> <listelement>element 3</listelement> <listelement xml:lang="sw">element 4</listelement> <listelement xml:lang="en-gb">element 5</listelement> <listelement xml:lang="zu">element 6</listelement> <listelement xml:lang="jz">element 7</listelement>
</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;">
<xsl:value-of select="list/title"/>
<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="."/> |
</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;">
title 1
element 1 |
element 2 |
element 3 |
element 4 |
element 5 |
element 6 |
element 7 |
</body>
</html></source>
generates a table with selected elements,with the number of elements per row given in the stylesheet
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="utf-8"?>
<element>Fe</element> <element>Cl</element> <element>Br</element> <element>I</element> <element>Ni</element> <element>H</element> <element>Po</element> <element>S</element> <element>O</element>
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="/">
</xsl:template> <xsl:template match="element"> <xsl:param name="inRow"/> <TR> <TD> <xsl:value-of select="."/> </TD> <xsl:apply-templates select="following::element[position() < $inRow]" mode="cell"/> </TR> </xsl:template> <xsl:template match="element" mode="cell"> <xsl:param name="inRow"/> <TD> <xsl:value-of select="."/> </TD> </xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?>Fe | Cl | Br |
I | Ni | H |
Po | S | O |
Fe | Cl | Br | I |
Ni | H | Po | S |
O |
Fe | Cl | Br | I | Ni |
H | Po | S | O |
Set table cell style with choose statement
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <list xml:lang="en">
<title>title 1</title> <listelement>element 1</listelement> <listelement>element 2</listelement> <listelement>element 3</listelement> <listelement xml:lang="sw">element 4</listelement> <listelement xml:lang="en-gb">element 5</listelement> <listelement xml:lang="zu">element 6</listelement> <listelement xml:lang="jz">element 7</listelement>
</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;">
<xsl:value-of select="list/title"/>
<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="."/> |
</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;">
title 1
element 1 |
element 2 |
element 3 |
element 4 |
element 5 |
element 6 |
element 7 |
</body>
</html></source>
Table cell format
<source lang="xml">
File: Data.xml <?xml version="1.0" ?> <transcript>
<student id="STU12345" name="name 1" status="active"> <home_address>35 Wall Street, Wonderland, NJ</home_address> <interests> <interest>interest 1</interest> <interest>interest 2</interest> <interest>interest 3</interest> </interests> </student> <term> <heading name="Winter 1999" /> <course> <course-name>course 1</course-name> <grade>A-</grade> <credits>4</credits> </course> <course> <course-name>course 2</course-name> <grade>B+</grade> <credits>3</credits> </course> </term> <summary>summary</summary> <comments> comments </comments>
</transcript>
File: Transform.xslt
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="transcript"> <HTML> <BODY> Student Transcript <P /> <xsl:apply-templates select="student" />
Course Name</TH>
<td>Grade</TH> <TH ALIGN="right">Credits</TH> |
Course Name | Grade | Credits |
Winter 1999 | ||
course 1 | A- | 4 |
course 2 | B+ | 3 |
</BODY>
</HTML></source>
Use for-each loop to output table row
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <INVENTORY>
<BOOK InStock="yes"> <TITLE>title 1</TITLE> <AUTHOR Born="1835">author 1</AUTHOR> <BINDING>paperback</BINDING> <PAGES>298</PAGES> <PRICE>$5.49</PRICE> </BOOK> <BOOK InStock="no"> <TITLE>Leaves of Grass</TITLE> <AUTHOR Born="1819">W</AUTHOR> <BINDING>hardcover</BINDING> <PAGES>462</PAGES> <PRICE>$7.75</PRICE> </BOOK>
</INVENTORY>
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> <HEAD> <TITLE>Books in Stock</TITLE> </HEAD> <BODY>
Books In Stock
Title</TH>
<td>Author</TH> <td>Binding Type</TH> <td>Number of Pages</TH> <td>Price</TH> </THEAD> <xsl:for-each select="INVENTORY/BOOK[@InStock="yes"]"> <TR ALIGN="CENTER"> <TD> <xsl:value-of select="TITLE"/> |
<TD>
<xsl:value-of select="AUTHOR"/>
Title</TH>
<td>Author</TH> <td>Binding Type</TH> <td>Number of Pages</TH> <td>Price</TH> </THEAD> <TR ALIGN="CENTER"><TD>title 1 |
<TD>author 1