XML/XSLT stylesheet/table

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

Add row number

   <source lang="xml">

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>

A list of books

<xsl:apply-templates />
     </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>

A list of books

1 author1 title 1 8.95
2 author 2 title 2 12.99
3 author 3 title 3 8.99
4 J. R. R. Tolkien The Lord of the Rings 22.99
  </body>

</html>

</source>
   
  


Create a table with sorting

   <source lang="xml">

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>

A list of books

<xsl:for-each select="//book"> <xsl:sort select="author" /> </xsl:for-each>
           <xsl:value-of select="author" />
           <xsl:value-of select="title" />
           <xsl:value-of select="@category" />
           <xsl:value-of select="price" />
 </body>

</html> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>A list of books</title>
  </head>
  <body>

A list of books

  </body>

</html>

</source>
   
  


Create table header

   <source lang="xml">

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>
<xsl:apply-templates select="customer-list/customer"> <xsl:sort select="name/last" /> <xsl:sort select="name/first" /> </xsl:apply-templates>
     </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>
D, C
order 3
order 4
B, A
order 1
order 2
  </BODY>

</HTML>

</source>
   
  


Create table header and content in separated templates

   <source lang="xml">

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>
<xsl:apply-templates />
Name E-mail Address
     </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>
Name E-mail Address
name 1 g@gmail.ru
name 2 n@hotmail.ru
  </body>

</html>

</source>
   
  


Fill more one value into table cell

   <source lang="xml">

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>
<xsl:apply-templates />
Number Name Hire Date Address Phone Fax Email
     </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" />
     
<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]" />
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>
Number Name Hire Date Address Phone Fax Email
1 JoeBrianSmith 2008-10-29 1 Drive
Vancouver
                    ,
                    BC
                    WK:
                    
HM:
303-4667357 a@a.ru
  </body>

</html>

</source>
   
  


for-each loop and table output

   <source lang="xml">

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

</html>

</source>
   
  


Generate two tables

   <source lang="xml">

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>
     
     Report of <xsl:value-of select="@type" /> by Customer
     
     <xsl:apply-templates select="customer" />  
   </BODY>
   </HTML>
   
 </xsl:template>
 <xsl:template match="customer">

Customer #: <xsl:value-of select="@number" /> Name: <xsl:value-of select="name" /> Status: <xsl:value-of select="@type" />
   <xsl:apply-templates select="order-list/order" />
 </xsl:template>
 <xsl:template match="order">
   <P/>
Order #: <xsl:value-of select="@number" /> Status: <xsl:value-of select="@status" />
<xsl:apply-templates select="item-list/item" />
Quantity Item Price
 </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>
              Report of Unfilled Orders by Customer

Customer #: CUST111 Name: name 1 Status: VIP

Order #: ORD200 Status: late
Quantity Item Price
5 item 1 100
2 item 2 50
1 item 3 50

Order #: ORD105 Status: backordered
Quantity Item Price
6 item 4 25

Customer #: CUST222 Name: Alice Liddle Status: normal

Order #: ORD102 Status: late
Quantity Item Price
20 item 1 100
10 item 2 50
10 item 3 50
10 item 4 25
2 item 5 120
  </BODY>

</HTML>

</source>
   
  


Get value with value-of for table cell

   <source lang="xml">

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

</html>

</source>
   
  


number column

   <source lang="xml">

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>
<xsl:for-each select="employee"> </xsl:for-each>
Number Name Email
<xsl:number/> <xsl:value-of select="contact/name/firstName"/> <xsl:value-of select="contact/email"/>
   </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
 
</source>
   
  


One template per table row

   <source lang="xml">

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>
<xsl:apply-templates />
Name E-mail Address
     </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>
Name E-mail Address
name 1 g@gmail.ru
name 2 n@hotmail.ru
  </body>

</html>

</source>
   
  


Output to a table

   <source lang="xml">

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

</html>

</source>
   
  


select value for table cell

   <source lang="xml">

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

</html>

</source>
   
  


Sort a column

   <source lang="xml">

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>
<xsl:for-each select="/library/DVD"> <xsl:sort select="genre"/> </xsl:for-each>
Title Format Genre
               <xsl:value-of select="title"/>
               <xsl:value-of select="format"/>
               <xsl:value-of select="genre"/>
     </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>
Title Format Genre
title 1 Movie Classic
House TV Series Comedy
Contact Movie Science fiction
  </body>

</html>

</source>
   
  


Sort first then output to table

   <source lang="xml">

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>
<xsl:apply-templates select="customer-list/customer"> <xsl:sort select="name/last"/> <xsl:sort select="name/first"/> </xsl:apply-templates>
     </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>
D, C
order 3
order 4
B, A
order 1
order 2
  </BODY>

</HTML>

</source>
   
  


use
to format value in a table cell

   <source lang="xml">

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>
<xsl:for-each select="employee"> </xsl:for-each>
Name Hire Date Address Phone Fax Email
               <xsl:value-of
                 select="contact/name/name" />
               <xsl:value-of
                 select="contact/name/middleName" />
               <xsl:value-of
                 select="contact/name/lastName" />
               <xsl:value-of select="hireDate" />
               <xsl:value-of
                 select="contact/address/street" />
               
<xsl:value-of select="contact/address/city" /> , <xsl:value-of select="contact/address/state" /> <xsl:value-of select="contact/address/zip" />
               WK:
               <xsl:value-of
                 select="contact/phone/tel[@type=wk]" />
               
HM: <xsl:value-of select="contact/phone/tel[@type=hm]" />
               <xsl:value-of
                 select="contact/phone/fax" />
               <xsl:value-of select="contact/email" />
     </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>
Name Hire Date Address Phone Fax Email
BrianSmith 2008-10-29 1 Drive
Vancouver
                              ,
                              BC
                              WK:
                              
HM:
303-4667357 a@a.ru
  </body>

</html>

</source>
   
  


Use for-each to loop through nodes in certain level

   <source lang="xml">

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>
<xsl:for-each select="emailList/person"> </xsl:for-each>
Name E-mail Address
<xsl:value-of select="name"/> <xsl:value-of select="email"/>
     </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>
Name E-mail Address
name 1 g@gmail.ru
name 2 n@hotmail.ru
  </body>

</html>

</source>
   
  


Use for-each to output table rows

   <source lang="xml">

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

</html>

</source>
   
  


Use xslt style sheet to output data in a table

   <source lang="xml">

<?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]">
       <xsl:apply-templates/>
Metadata for this Resource: <xsl:value-of select="@name"/>
PropertyTypeValue
   </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 > 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>

</source>