XML Tutorial/XSLT stylesheet/table

Материал из Web эксперт
Версия от 18:22, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Format table cell with choose statement

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;">
        <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/listelement">
            <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;">element 1</td>
         </tr>
         <tr>
            <td style="background: white; color: black;">element 2</td>
         </tr>
         <tr>
            <td style="background: black;">element 3</td>
         </tr>
         <tr>
            <td style="background: yellow; color: black;">element 4</td>
         </tr>
         <tr>
            <td style="background: blue;">element 5</td>
         </tr>
         <tr>
            <td style="background: white; color: black;">element 6</td>
         </tr>
         <tr>
            <td style="background: black;">element 7</td>
         </tr>
      </table>
   </body>
</html>


generates a table with selected elements,with the number of elements per row given in the stylesheet

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <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>
</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">
        <xsl:variable name="inRow" select="3"/>
        <xsl:apply-templates select="//element[position() mod $inRow = 1]">
          <xsl:with-param name="inRow" select="$inRow"/>
        </xsl:apply-templates>
      </TABLE>
      <TABLE border="1">
        <xsl:variable name="inRow" select="4"/>
        <xsl:apply-templates select="//element[position() mod $inRow = 1]">
          <xsl:with-param name="inRow" select="$inRow"/>
        </xsl:apply-templates>
      </TABLE>
      <TABLE border="1">
        <xsl:variable name="inRow" select="5"/>
        <xsl:apply-templates select="//element[position() mod $inRow = 1]">
          <xsl:with-param name="inRow" select="$inRow"/>
        </xsl:apply-templates>
      </TABLE>
    </xsl:template>
    <xsl:template match="element">
      <xsl:param name="inRow"/>
      <TR>
        <TD>
          <xsl:value-of select="."/>
        </TD>
        <xsl:apply-templates select="following::element[position() &lt; $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"?><TABLE border="1"><TR><TD>Fe</TD><TD>Cl</TD><TD>Br</TD></TR><TR><TD>I</TD><TD>Ni</TD><TD>H</TD></TR><TR><TD>Po</TD><TD>S</TD><TD>O</TD></TR></TABLE><TABLE border="1"><TR><TD>Fe</TD><TD>Cl</TD><TD>Br</TD><TD>I</TD></TR><TR><TD>Ni</TD><TD>H</TD><TD>Po</TD><TD>S</TD></TR><TR><TD>O</TD></TR></TABLE><TABLE border="1"><TR><TD>Fe</TD><TD>Cl</TD><TD>Br</TD><TD>I</TD><TD>Ni</TD></TR><TR><TD>H</TD><TD>Po</TD><TD>S</TD><TD>O</TD></TR></TABLE>


Set table cell style with choose statement

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;">
        <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/listelement">
            <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;">element 1</td>
         </tr>
         <tr>
            <td style="background: white; color: black;">element 2</td>
         </tr>
         <tr>
            <td style="background: black;">element 3</td>
         </tr>
         <tr>
            <td style="background: yellow; color: black;">element 4</td>
         </tr>
         <tr>
            <td style="background: blue;">element 5</td>
         </tr>
         <tr>
            <td style="background: white; color: black;">element 6</td>
         </tr>
         <tr>
            <td style="background: black;">element 7</td>
         </tr>
      </table>
   </body>
</html>


Table cell format

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>
        <FONT SIZE="6">
          <B>Student Transcript</B>
        </FONT>
        <P />
        <xsl:apply-templates select="student" />
        <HR />
        <TABLE ALIGN="left" BORDER="1" CELLPADDING="4">
          <TR>
            <td>Course Name</TH>
            <td>Grade</TH>
            <TH ALIGN="right">Credits</TH>
          </TR>
          <xsl:apply-templates select="term" />
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
  <xsl:template match="student">
    <FONT SIZE="4">
      <B>
        Name:
        <I>
          <xsl:value-of select="@name" />
        </I>
        <BR />
        ID:
        <I>
          <xsl:value-of select="@number" />
        </I>
      </B>
    </FONT>
    <P />
  </xsl:template>
  <xsl:template match="term">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="heading">
    <TR>
      <TH COLSPAN="3">
        <xsl:value-of select="@name" />
      </TH>
    </TR>
  </xsl:template>
  <xsl:template match="course">
    <TR>
      <TD>
        <xsl:value-of select="course-name" />
      </TD>
      <TD>
        <xsl:value-of select="grade" />
      </TD>
      <TD ALIGN="right">
        <xsl:value-of select="credits" />
      </TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>
Output:
<HTML>
   <BODY><FONT SIZE="6"><B>Student Transcript</B></FONT><Paragraph></Paragraph><FONT SIZE="4"><B>
                    Name:
                    <I>name 1</I><BR>
                    ID:
                    <I></I></B></FONT><Paragraph></Paragraph>
      <HR>
      <TABLE ALIGN="left" BORDER="1" CELLPADDING="4">
         <TR>
            <Td>Course Name</Td>
            <Td>Grade</Td>
            <Td ALIGN="right">Credits</Td>
         </TR>
             
         <TR>
            <Td COLSPAN="3">Winter 1999</Td>
         </TR>
             
         <TR>
            <TD>course 1</TD>
            <TD>A-</TD>
            <TD ALIGN="right">4</TD>
         </TR>
             
         <TR>
            <TD>course 2</TD>
            <TD>B+</TD>
            <TD ALIGN="right">3</TD>
         </TR>
           
      </TABLE>
   </BODY>
</HTML>


Use for-each loop to output table row

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>
      <H2>Books In Stock</H2>
      <TABLE BORDER="1" CELLPADDING="5">
         <THEAD>
            <td>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>
               <TD>
                  <xsl:value-of select="AUTHOR"/> <BR/>
                  (born <xsl:value-of select="AUTHOR/@Born"/>)
               </TD>
               <TD>
                  <xsl:value-of select="BINDING"/>
               </TD>
               <TD>
                  <xsl:value-of select="PAGES"/>
               </TD>
               <TD>
                  <xsl:value-of select="PRICE"/>
               </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>Books in Stock</TITLE>
   </HEAD>
   <BODY>
      <H2>Books In Stock</H2>
      <TABLE BORDER="1" CELLPADDING="5">
         <THEAD>
            <td>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>
            <TD>author 1<BR>
               (born 1835)
               
            </TD>
            <TD>paperback</TD>
            <TD>298</TD>
            <TD>$5.49</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>