XML/XSLT stylesheet/table — различия между версиями

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

Текущая версия на 08:26, 26 мая 2010

Add row number

File: Data.xml 
<?xml version="1.0"?>
<books>
  <book category="reference">
      <author>author1</author>
      <title>title 1</title>
      <price>8.95</price>
   </book>
   <book category="fiction">
      <author>author 2</author>
      <title>title 2</title>
      <price>12.99</price>
   </book>
   <book category="fiction">
      <author>author 3</author>
      <title>title 3</title>
      <price>8.99</price>
   </book>
   <book category="fiction">
      <author>J. R. R. Tolkien</author>
      <title>The Lord of the Rings</title>
      <price>22.99</price>
   </book>
</books>

File: Transform.xslt
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:template match="books">
    <html>
      <body>
        <h1>A list of books</h1>
        <table width="640">
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="book">
    <tr>
      <td>
        <xsl:number />
      </td>
      <td>
        <xsl:value-of select="author" />
      </td>
      <td>
        <xsl:value-of select="title" />
      </td>
      <td>
        <xsl:value-of select="price" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <h1>A list of books</h1>
      <table width="640">
         
         <tr>
            <td>1</td>
            <td>author1</td>
            <td>title 1</td>
            <td>8.95</td>
         </tr>
         
         <tr>
            <td>2</td>
            <td>author 2</td>
            <td>title 2</td>
            <td>12.99</td>
         </tr>
         
         <tr>
            <td>3</td>
            <td>author 3</td>
            <td>title 3</td>
            <td>8.99</td>
         </tr>
         
         <tr>
            <td>4</td>
            <td>J. R. R. Tolkien</td>
            <td>The Lord of the Rings</td>
            <td>22.99</td>
         </tr>
         
      </table>
   </body>
</html>



Create a table with sorting

File: Data.xml 

<?xml version="1.0" encoding="iso-8859-1"?>
<document>
    <author>author</author>
    <title>XSLT 2.0 Programmer"s Reference</title>
    <copyright/>
    <date/>
    <abstract>abstract
    </abstract>
</document>

File: Transform.xslt
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xsl:version="1.0">
  <head>
    <title>A list of books</title>
  </head>
  <body>
    <h1>A list of books</h1>
    <table border="2">
      <xsl:for-each select="//book">
        <xsl:sort select="author" />
        <tr>
          <td>
            <xsl:value-of select="author" />
          </td>
          <td>
            <xsl:value-of select="title" />
          </td>
          <td>
            <xsl:value-of select="@category" />
          </td>
          <td>
            <xsl:value-of select="price" />
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
</html>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>A list of books</title>
   </head>
   <body>
      <h1>A list of books</h1>
      <table border="2"></table>
   </body>
</html>



Create table header

File: Transform.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <HTML>
      <HEAD>
        <TITLE>Customers</TITLE>
      </HEAD>
      <BODY>
        <TABLE BORDER="1">
          <xsl:apply-templates
            select="customer-list/customer">
            <xsl:sort select="name/last" />
            <xsl:sort select="name/first" />
          </xsl:apply-templates>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
  <xsl:template match="customer">
    <TR>
      <TH ALIGN="left">
        <xsl:value-of select="name/last" />
        <xsl:text>, </xsl:text>
        <xsl:value-of select="name/first" />
      </TH>
    </TR>
    <xsl:apply-templates select="order" />
  </xsl:template>
  <xsl:template match="order">
    <TR>
      <TD>
        <xsl:apply-templates />
      </TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>
File:  Data.xml
<?xml version="1.0" ?>
<customer-list>
  <customer>
    <name>
      <first>A</first>
      <last>B</last>
    </name>
    <order>order 1</order>
    <order>order 2</order>
  </customer>
  <customer>
    <name>
      <first>C</first>
      <last>D</last>
    </name>
    <order>order 3</order>
    <order>order 4</order>
  </customer>
</customer-list>
Output:
<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE>Customers</TITLE>
   </HEAD>
   <BODY>
      <TABLE BORDER="1">
         <TR>
            <TH ALIGN="left">D, C</TH>
         </TR>
         <TR>
            <TD>order 3</TD>
         </TR>
         <TR>
            <TD>order 4</TD>
         </TR>
         <TR>
            <TH ALIGN="left">B, A</TH>
         </TR>
         <TR>
            <TD>order 1</TD>
         </TR>
         <TR>
            <TD>order 2</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>



Create table header and content in separated templates

File: Data.xml
<emailList>
  <person>
    <name>name 1</name>
    <email>g@gmail.ru</email>
  </person>
  <person>
    <name>name 2</name>
    <email>n@hotmail.ru</email>
  </person>
</emailList>
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="/">
    <html>
      <head>
        <title>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="emailList">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="person">
    <tr>
      <td>
        <xsl:value-of select="name" />
      </td>
      <td>
        <xsl:value-of select="email" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
           
         <tr>
            <td>name 1</td>
            <td>g@gmail.ru</td>
         </tr>
           
         <tr>
            <td>name 2</td>
            <td>n@hotmail.ru</td>
         </tr>
         
      </table>
   </body>
</html>



Fill more one value into table cell

File: Data.xml
<employees>
      <employee eid="98145" dept="programming">
        <title>Java Programmer</title>
        <contact addInfo="info1">
          <name>
            <firstName>Joe</firstName>
            <middleName int="B">Brian</middleName>
            <lastName>Smith</lastName>
          </name>
          <address>
            <street>1 Drive</street>
            <city>Vancouver</city>
            <state>BC</state>
            <zipcode>80210</zipcode>
          </address>
          <phone>
            <tel type="wk">111-1111111</tel>
            <tel type="hm">222-222222</tel>
            <fax>303-4667357</fax>
          </phone>
          <email>a@a.ru</email>
        </contact>
        <hireDate>2008-10-29</hireDate>
      </employee>
   
    </employees>
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="employees">
    <html>
      <head>
        <title>Employee Data</title>
      </head>
      <body>
        <table cellpadding="5" bgcolor="#cccccc">
          <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
          </tr>
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="employee">
    <tr>
      <td>
        <xsl:number />
      </td>
      <xsl:apply-templates select="contact" />
    </tr>
  </xsl:template>
  <xsl:template match="contact">
    <td>
      <xsl:value-of select="name/firstName" />
      <xsl:value-of select="name/middleName" />
      <xsl:value-of select="name/lastName" />
    </td>
    <td>
      <xsl:value-of select="../hireDate" />
    </td>
    <td>
      <xsl:value-of select="address/street" />
      <br />
      <xsl:value-of select="address/city" />
      ,
      <xsl:value-of select="address/state" />
      <xsl:value-of select="address/zip" />
    </td>
    <td>
      WK:
      <xsl:value-of select="phone/tel[@type=wk]" />
      <br />
      HM:
      <xsl:value-of select="phone/tel[@type=hm]" />
    </td>
    <td>
      <xsl:value-of select="phone/fax" />
    </td>
    <td>
      <xsl:value-of select="email" />
    </td>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Employee Data</title>
   </head>
   <body>
      <table cellpadding="5" bgcolor="#cccccc">
         <tr>
            <th>Number</th>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
         </tr>
         
         <tr>
            <td>1</td>
            <td>JoeBrianSmith</td>
            <td>2008-10-29</td>
            <td>1 Drive<br>Vancouver
                     ,
                     BC
            </td>
            <td>
                     WK:
                     <br>
                     HM:
                     
            </td>
            <td>303-4667357</td>
            <td>a@a.ru</td>
         </tr>
         
         
      </table>
   </body>
</html>



for-each loop and table output

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>
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>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="email"/></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>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>person1</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person2</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person3</td>
            <td>p3@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



Generate two tables

File: Data.xml
<?xml version="1.0" standalone="yes"?>
<report type="Unfilled Orders">
  <customer number="CUST111" type="VIP">
    <name>name 1</name>
    <order-list count="2">
      <order number="ORD200" owner="CUST111" total="650.00" status="late">
        <item-list>
          <item quantity="5" price="100">item 1</item>
          <item quantity="2" price="50">item 2</item>
          <item quantity="1" price="50">item 3</item>
        </item-list>
      </order>
      <order number="ORD105" owner="CUST111" total="150.00" status="backordered">
        <item-list>
          <item quantity="6" price="25">item 4</item>
        </item-list>
      </order>
    </order-list>
  </customer>
  <customer number="CUST222" type="normal">
    <name>Alice Liddle</name>
    <order-list count="2">
      <order number="ORD102" owner="CUST222" total="3490.00" status="late">
        <item-list>
          <item quantity="20" price="100">item 1</item>
          <item quantity="10" price="50">item 2</item>
          <item quantity="10" price="50">item 3</item>
          <item quantity="10" price="25">item 4</item>
          <item quantity="2" price="120">item 5</item>
        </item-list>
      </order>
    </order-list>
  </customer>
</report>

File: Transform.xslt
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="report">
    <HTML>
    <BODY>
      <FONT SIZE="6">
      Report of <xsl:value-of select="@type" /> by Customer
      </FONT>
      <xsl:apply-templates select="customer" />  
    </BODY>
    </HTML>
    
  </xsl:template>
  <xsl:template match="customer">
    <HR/>
    <TABLE BORDER="1" CELLPADDING="3">
      <TR ALIGN="left">
        <TH>Customer #:</TH>
        <TD><xsl:value-of select="@number" /></TD>
        <TH>Name:</TH>
        <TD><xsl:value-of select="name" /></TD>
        <TH>Status:</TH>
        <TD><xsl:value-of select="@type" /></TD>
      </TR>
    </TABLE>
    
    <xsl:apply-templates select="order-list/order" />
  </xsl:template>
  <xsl:template match="order">
    <P/>
    <TABLE BORDER="1" CELLPADDING="3">
      <TR ALIGN="left">
        <TH>Order #:</TH>
        <TD><xsl:value-of select="@number" /></TD>
        <TH>Status:</TH>
        <TD><xsl:value-of select="@status" /></TD>
      </TR>
    </TABLE>
    <TABLE BORDER="1" CELLPADDING="3" WIDTH="100%">
      <TR ALIGN="left">
        <TH>Quantity</TH>
        <TH>Item</TH>
        <TH ALIGN="right">Price</TH>
      </TR>
      <xsl:apply-templates select="item-list/item" />
    </TABLE>
    
  </xsl:template>
  <xsl:template match="item">
    <TR ALIGN="left">
      <TD><xsl:value-of select="@quantity" /></TD>
      <TD><xsl:value-of select="text()" /></TD>
      <TD ALIGN="right"><xsl:value-of select="@price" /></TD>
    </TR>
  </xsl:template>
      
</xsl:stylesheet>
Output:
<HTML>
   <BODY><FONT SIZE="6">
               Report of Unfilled Orders by Customer
               </FONT><HR>
      <TABLE BORDER="1" CELLPADDING="3">
         <TR ALIGN="left">
            <TH>Customer #:</TH>
            <TD>CUST111</TD>
            <TH>Name:</TH>
            <TD>name 1</TD>
            <TH>Status:</TH>
            <TD>VIP</TD>
         </TR>
      </TABLE>
      <P></P>
      <TABLE BORDER="1" CELLPADDING="3">
         <TR ALIGN="left">
            <TH>Order #:</TH>
            <TD>ORD200</TD>
            <TH>Status:</TH>
            <TD>late</TD>
         </TR>
      </TABLE>
      <TABLE BORDER="1" CELLPADDING="3" WIDTH="100%">
         <TR ALIGN="left">
            <TH>Quantity</TH>
            <TH>Item</TH>
            <TH ALIGN="right">Price</TH>
         </TR>
         <TR ALIGN="left">
            <TD>5</TD>
            <TD>item 1</TD>
            <TD ALIGN="right">100</TD>
         </TR>
         <TR ALIGN="left">
            <TD>2</TD>
            <TD>item 2</TD>
            <TD ALIGN="right">50</TD>
         </TR>
         <TR ALIGN="left">
            <TD>1</TD>
            <TD>item 3</TD>
            <TD ALIGN="right">50</TD>
         </TR>
      </TABLE>
      <P></P>
      <TABLE BORDER="1" CELLPADDING="3">
         <TR ALIGN="left">
            <TH>Order #:</TH>
            <TD>ORD105</TD>
            <TH>Status:</TH>
            <TD>backordered</TD>
         </TR>
      </TABLE>
      <TABLE BORDER="1" CELLPADDING="3" WIDTH="100%">
         <TR ALIGN="left">
            <TH>Quantity</TH>
            <TH>Item</TH>
            <TH ALIGN="right">Price</TH>
         </TR>
         <TR ALIGN="left">
            <TD>6</TD>
            <TD>item 4</TD>
            <TD ALIGN="right">25</TD>
         </TR>
      </TABLE>
      <HR>
      <TABLE BORDER="1" CELLPADDING="3">
         <TR ALIGN="left">
            <TH>Customer #:</TH>
            <TD>CUST222</TD>
            <TH>Name:</TH>
            <TD>Alice Liddle</TD>
            <TH>Status:</TH>
            <TD>normal</TD>
         </TR>
      </TABLE>
      <P></P>
      <TABLE BORDER="1" CELLPADDING="3">
         <TR ALIGN="left">
            <TH>Order #:</TH>
            <TD>ORD102</TD>
            <TH>Status:</TH>
            <TD>late</TD>
         </TR>
      </TABLE>
      <TABLE BORDER="1" CELLPADDING="3" WIDTH="100%">
         <TR ALIGN="left">
            <TH>Quantity</TH>
            <TH>Item</TH>
            <TH ALIGN="right">Price</TH>
         </TR>
         <TR ALIGN="left">
            <TD>20</TD>
            <TD>item 1</TD>
            <TD ALIGN="right">100</TD>
         </TR>
         <TR ALIGN="left">
            <TD>10</TD>
            <TD>item 2</TD>
            <TD ALIGN="right">50</TD>
         </TR>
         <TR ALIGN="left">
            <TD>10</TD>
            <TD>item 3</TD>
            <TD ALIGN="right">50</TD>
         </TR>
         <TR ALIGN="left">
            <TD>10</TD>
            <TD>item 4</TD>
            <TD ALIGN="right">25</TD>
         </TR>
         <TR ALIGN="left">
            <TD>2</TD>
            <TD>item 5</TD>
            <TD ALIGN="right">120</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>



Get value with value-of for table cell

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>
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>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="email"/></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>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>person1</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person2</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person3</td>
            <td>p3@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



number column

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<employees xmlns="http://www.domain.ru/namespace/employee">
  <title>Employee Data File</title>
  <employee eid="1" dept="programming">
    <contact addInfo="info1">
      <name>
        <firstName>Joe</firstName>
        <middleName int="B">Brian</middleName>
        <lastName>Smith</lastName>
      </name>
      <address>
        <street>1 Drive</street>
        <city>Vancouver</city>
        <state>BC</state>
        <zipcode>80210</zipcode>
      </address>
      <phone>
        <tel type="wk">111-1111111</tel>
        <tel type="hm">222-222222</tel>
        <fax>303-4667357</fax>
      </phone>
      <email>a@a.ru</email>
    </contact>
    <hireDate>2008-10-29</hireDate>
  </employee>
</employees>
File: Transform.xslt
<xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
<xsl:template match="employees">
  <html>
    <head>
      <title>Employee Data Output</title>
    </head>
    <body>
      <table>
        <tr>
          <th>Number</th>
          <th>Name</th>
          <th>Email</th>
        </tr>
        
          <xsl:for-each select="employee">
          <tr>
            <td><xsl:number/></td>
            <td><xsl:value-of select="contact/name/firstName"/></td>
            <td><xsl:value-of select="contact/email"/></td>
            </tr>
          </xsl:for-each>
        
      </table>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  Employee Data File
  
    
      
        Joe
        Brian
        Smith
      
      
        1 Drive
        Vancouver
        BC
        80210
      
      
        111-1111111
        222-222222
        303-4667357
      
      a@a.ru
    
    2008-10-29



One template per table row

File: Data.xml
<emailList>
  <person>
    <name>name 1</name>
    <email>g@gmail.ru</email>
  </person>
  <person>
    <name>name 2</name>
    <email>n@hotmail.ru</email>
  </person>
</emailList>
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="/">
    <html>
      <head>
        <title>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="person">
    <tr>
      <td>
        <xsl:value-of select="name" />
      </td>
      <td>
        <xsl:value-of select="email" />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
           
         <tr>
            <td>name 1</td>
            <td>g@gmail.ru</td>
         </tr>
           
         <tr>
            <td>name 2</td>
            <td>n@hotmail.ru</td>
         </tr>
         
      </table>
   </body>
</html>



Output to a table

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>

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>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="email"/></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>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>person1</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person2</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person3</td>
            <td>p3@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



select value for table cell

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>
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>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="email"/></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>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>person1</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person2</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person3</td>
            <td>p3@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



Sort a column

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<library>
  <DVD id="1">
    <title>title 1</title>
    <format>Movie</format>
    <genre>Classic</genre>
  </DVD>
  <DVD id="2">
    <title>Contact</title>
    <format>Movie</format>
    <genre>Science fiction</genre>
  </DVD>
  <DVD id="3">
    <title>House</title>
    <format>TV Series</format>
    <genre>Comedy</genre>
  </DVD>
</library>
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" version="4.0"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>DVD Library Listing</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
      </head>
      <body>
        <table border="1">
          <tr>
            <th>Title</th>
            <th>Format</th>
            <th>Genre</th>
          </tr>
          <xsl:for-each select="/library/DVD">
            <xsl:sort select="genre"/>
            <tr>
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="format"/>
              </td>
              <td>
                <xsl:value-of select="genre"/>
              </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>DVD Library Listing</title>
      <link rel="stylesheet" type="text/css" href="style.css">
   </head>
   <body>
      <table border="1">
         <tr>
            <th>Title</th>
            <th>Format</th>
            <th>Genre</th>
         </tr>
         <tr>
            <td>title 1</td>
            <td>Movie</td>
            <td>Classic</td>
         </tr>
         <tr>
            <td>House</td>
            <td>TV Series</td>
            <td>Comedy</td>
         </tr>
         <tr>
            <td>Contact</td>
            <td>Movie</td>
            <td>Science fiction</td>
         </tr>
      </table>
   </body>
</html>



Sort first then output to table

File: Data.xml
<?xml version="1.0" ?>
<customer-list>
  <customer>
    <name>
      <first>A</first>
      <last>B</last>
    </name>
    <order>order 1</order>
    <order>order 2</order>
  </customer>
  <customer>
    <name>
      <first>C</first>
      <last>D</last>
    </name>
    <order>order 3</order>
    <order>order 4</order>
  </customer>
</customer-list>
File: Transform.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <HTML>
      <HEAD><TITLE>Customers</TITLE></HEAD>
      <BODY>
        <TABLE BORDER="1">
          <xsl:apply-templates select="customer-list/customer">            
            <xsl:sort select="name/last"/>
            <xsl:sort select="name/first"/>
          </xsl:apply-templates>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
  <xsl:template match="customer">
    <TR>
      <TH ALIGN="left">
        <xsl:value-of select="name/last"/>
        <xsl:text>, </xsl:text>
        <xsl:value-of select="name/first"/>
      </TH>
    </TR>
    <xsl:apply-templates select="order"/>
  </xsl:template>
  <xsl:template match="order">
    <TR>
      <TD>
        <xsl:apply-templates/>
      </TD>
    </TR>
  </xsl:template>
</xsl:stylesheet>
Output:
<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE>Customers</TITLE>
   </HEAD>
   <BODY>
      <TABLE BORDER="1">
         <TR>
            <TH ALIGN="left">D, C</TH>
         </TR>
         <TR>
            <TD>order 3</TD>
         </TR>
         <TR>
            <TD>order 4</TD>
         </TR>
         <TR>
            <TH ALIGN="left">B, A</TH>
         </TR>
         <TR>
            <TD>order 1</TD>
         </TR>
         <TR>
            <TD>order 2</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>



use
to format value in a table cell

File: Data.xml

<employees>
  <employee eid="98145" dept="programming">
    <title>Java Programmer</title>
    <contact addInfo="info1">
      <name>
        <firstName>Joe</firstName>
        <middleName int="B">Brian</middleName>
        <lastName>Smith</lastName>
      </name>
      <address>
        <street>1 Drive</street>
        <city>Vancouver</city>
        <state>BC</state>
        <zipcode>80210</zipcode>
      </address>
      <phone>
        <tel type="wk">111-1111111</tel>
        <tel type="hm">222-222222</tel>
        <fax>303-4667357</fax>
      </phone>
      <email>a@a.ru</email>
    </contact>
    <hireDate>2008-10-29</hireDate>
  </employee>
</employees>
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="employees">
    <html>
      <head>
        <title>Employee Data</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
          </tr>
          <xsl:for-each select="employee">
            <tr>
              <td>
                <xsl:value-of
                  select="contact/name/name" />
                <xsl:value-of
                  select="contact/name/middleName" />
                <xsl:value-of
                  select="contact/name/lastName" />
              </td>
              <td>
                <xsl:value-of select="hireDate" />
              </td>
              <td>
                <xsl:value-of
                  select="contact/address/street" />
                <br />
                <xsl:value-of
                  select="contact/address/city" />
                ,
                <xsl:value-of
                  select="contact/address/state" />
                <xsl:value-of
                  select="contact/address/zip" />
              </td>
              <td>
                WK:
                <xsl:value-of
                  select="contact/phone/tel[@type=wk]" />
                <br />
                HM:
                <xsl:value-of
                  select="contact/phone/tel[@type=hm]" />
              </td>
              <td>
                <xsl:value-of
                  select="contact/phone/fax" />
              </td>
              <td>
                <xsl:value-of select="contact/email" />
              </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>Employee Data</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>Hire Date</th>
            <th>Address</th>
            <th>Phone</th>
            <th>Fax</th>
            <th>Email</th>
         </tr>
         <tr>
            <td>BrianSmith</td>
            <td>2008-10-29</td>
            <td>1 Drive<br>Vancouver
                               ,
                               BC
            </td>
            <td>
                               WK:
                               <br>
                               HM:
                               
            </td>
            <td>303-4667357</td>
            <td>a@a.ru</td>
         </tr>
      </table>
   </body>
</html>



Use for-each to loop through nodes in certain level

File: Data.xml
<emailList>
  <person>
    <name>name 1</name>
    <email>g@gmail.ru</email>
  </person>
  <person>
    <name>name 2</name>
    <email>n@hotmail.ru</email>
  </person>
</emailList>
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="/">
    <html>
      <head>
        <title>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="email"/></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>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>name 1</td>
            <td>g@gmail.ru</td>
         </tr>
         <tr>
            <td>name 2</td>
            <td>n@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



Use for-each to output table rows

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>
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>Email Listing</title>
      </head>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>E-mail Address</th>
          </tr>
          <xsl:for-each select="emailList/person">
            <tr>
              <td>
                <xsl:value-of select="name" />
              </td>
              <td>
                <xsl:value-of select="email" />
              </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>Email Listing</title>
   </head>
   <body>
      <table>
         <tr>
            <th>Name</th>
            <th>E-mail Address</th>
         </tr>
         <tr>
            <td>person1</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person2</td>
            <td>p@hotmail.ru</td>
         </tr>
         <tr>
            <td>person3</td>
            <td>p3@hotmail.ru</td>
         </tr>
      </table>
   </body>
</html>



Use xslt style sheet to output data in a table

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
                xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
                version="1.0">
 
    <xsl:output method="html"/>
                    
    <xsl:variable name="schemaLocation" select="substring-after(/*/@xsi:schemaLocation," ")"/>
    <xsl:variable name="schema" select="document($schemaLocation)"/>
    <xsl:variable name="instance" select="/"/>
    <xsl:template match="/">
        <HTML>
            <HEAD>
                <TITLE>Welcome</TITLE>
            </HEAD>
            <BODY>
                <xsl:apply-templates select="$schema//xsd:complexType[@name]"/>
            </BODY>
        </HTML>
    </xsl:template>
    <xsl:template match="xsd:complexType[@name]">
        <table border="1" width="50%">
            <tr><th colspan="3" align="center">Metadata for this Resource: <xsl:value-of select="@name"/></th></tr>
            <tr><th>Property</th><th>Type</th><th>Value</th></tr>
        <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="xsd:element">    
        <xsl:variable name="name" select="@name"/>   
        <xsl:variable name="type" select="@type"/>
        <xsl:if test="not(@maxOccurs)">
            <tr>
                <td align="center"><xsl:value-of select="$name"/></td>
                <td align="center"><xsl:value-of select="$type"/></td>
                <td align="center"><xsl:value-of select="$instance//*[name(.)=$name]"/></td>
            </tr>
        </xsl:if>   
        <xsl:if test="@maxOccurs">   
            <xsl:if test="@maxOccurs=1">
                <tr>
                    <td align="center"><xsl:value-of select="$name"/></td>
                    <td align="center"><xsl:value-of select="$type"/></td>
                    <td align="center"><xsl:value-of select="$instance//*[name(.)=$name]"/></td>
                </tr>
            </xsl:if>    
            <xsl:if test="@maxOccurs &gt; 1">
                <xsl:for-each select="$instance//*[name(.)=$name]">
                    <tr>
                        <td align="center"><xsl:value-of select="$name"/></td>
                        <td align="center"><xsl:value-of select="$type"/></td>
                        <td align="center"><xsl:value-of select="."/></td>
                    </tr>
                </xsl:for-each>
            </xsl:if>      
            <xsl:if test="@maxOccurs="unbounded"">
                <xsl:for-each select="$instance//*[name(.)=$name]">
                    <tr>
                        <td align="center"><xsl:value-of select="$name"/></td>
                        <td align="center"><xsl:value-of select="$type"/></td>
                        <td align="center"><xsl:value-of select="."/></td>
                    </tr>
                </xsl:for-each>
            </xsl:if>  
        </xsl:if>  
    </xsl:template>
</xsl:stylesheet>