XML Tutorial/XSLT stylesheet/table

Материал из Web эксперт
Перейти к: навигация, поиск

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:for-each select="list/listelement"> </xsl:for-each>
               <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:variable name="inRow" select="3"/> <xsl:apply-templates select="//element[position() mod $inRow = 1]"> <xsl:with-param name="inRow" select="$inRow"/> </xsl:apply-templates>
<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>
<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>
   </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"?>
FeClBr
INiH
PoSO
FeClBrI
NiHPoS
O
FeClBrINi
HPoSO
</source>


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:for-each select="list/listelement"> </xsl:for-each>
               <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" />

<xsl:apply-templates select="term" /> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="student"> Name: <xsl:value-of select="@name" />
ID: <xsl:value-of select="@number" />
<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>Student Transcript<Paragraph></Paragraph> Name: name 1
ID:
<Paragraph></Paragraph>
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

<THEAD> <TD> <xsl:value-of select="AUTHOR"/>
(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>

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"/>
<THEAD> <TD>author 1
(born 1835) </TD> <TD>paperback</TD> <TD>298</TD> <TD>$5.49</TD> </TR> </TABLE> </BODY> </HTML></source>
Title</TH>
           <td>Author</TH>
           <td>Binding Type</TH>
           <td>Number of Pages</TH>
           <td>Price</TH>
        </THEAD>
        <TR ALIGN="CENTER">
<TD>title 1