XML/XSLT stylesheet/html

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

Add more format with html tags

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="no" ?> <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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

 <xsl:template match="transcript">
   <HTML><BODY>
   <xsl:apply-templates select="student" />

<xsl:for-each select="term/course"> <xsl:sort select="credits" /> <xsl:apply-templates select="." /> </xsl:for-each>
Course Name Grade Credits
   </BODY></HTML>
 </xsl:template>
 <xsl:template match="student">
   Student Transcript<P/>
   Name: 
     <xsl:value-of select="@name" />
     
ID: <xsl:value-of select="@id" />
<P/> </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

Name: name 1
ID: STU12345


Course Name Grade Credits
course 2 B+ 3
course 1 A- 4
  </BODY>

</HTML>

</source>
   
  


Construct image name used by generated HTML in style sheet

   <source lang="xml">

File: Data.xml <?xml version="1.0" ?> <tables>

<table-name>Conference</table-name> <number-of-legs>4</number-of-legs> <table-top-material type="laminate">Ash</table-top-material> <table-shape>Oblong</table-shape> <retail-price currency="USD">1485</retail-price>

</tables>

File: Transform.xslt <?xml version="1.0" encoding="iso-8859-1" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="/">
   <html>
     <head>
       <title>Three Real Tables</title>
       <style type="text/css">
         .tname
         {font-family:Tahoma;font-size:14pt;font-weight:bold}
         .tdesc {font-family:Tahoma;font-size:10pt} .tsaletxt
         {font-family:Tahoma;font-size:14pt;font-weight:bold;color:gray;}
         .tprice
         {font-family:Tahoma;font-size:18pt;font-weight:bold;color:red;text-align:center}
       </style>
     </head>
     <body>
       <xsl:apply-templates select="/tables/table">
         <xsl:sort select="table-name" />
       </xsl:apply-templates>
     </body>
   </html>
 </xsl:template>
 <xsl:template match="table">
         <img src="{table-name}.gif"
           alt="The {table-name} Table" />
           The "
           <xsl:value-of select="table-name" />
           " Table
         <p />
           A useful
           <xsl:value-of select="number-of-legs" />
           -leg
           <xsl:value-of select="table-shape" />
           table with easy
           
to clean <xsl:value-of select="table-top-material" /> -effect <xsl:value-of select="table-top-material/@type" /> top.
         <p />
OUR SALE PRICE ONLY
         <xsl:apply-templates select="retail-price" />
   <p />
 </xsl:template>
 <xsl:template match="retail-price">
     <xsl:choose>
       <xsl:when test="@currency = "USD"">$</xsl:when>
       <xsl:when test="@currency = "GBP"">?</xsl:when>
       <xsl:when test="@currency = "EURO"">E</xsl:when>
       <xsl:when test="@currency = "YEN"">Y</xsl:when>
     </xsl:choose>
     <xsl:value-of select="format-number(., "#,##0.00")" />
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>Three Real Tables</title><style type="text/css">
         .tname
         {font-family:Tahoma;font-size:14pt;font-weight:bold}
         .tdesc {font-family:Tahoma;font-size:10pt} .tsaletxt
         {font-family:Tahoma;font-size:14pt;font-weight:bold;color:gray;}
         .tprice
         {font-family:Tahoma;font-size:18pt;font-weight:bold;color:red;text-align:center}
       </style></head>
  <body>
<img src="Conference.gif" alt="The Conference Table">
                             The "
                             Conference
                             " Table
                           

                             A useful
                             4
                             -leg
                             Oblong
                             table with easy
                             
to clean Ash -effect laminate top.

OUR SALE PRICE ONLY
$1,485.00

  </body>

</html>

</source>
   
  


Format output with font

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="no" ?> <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>
 <term>
   <heading name="Spring 1999" />
   <course>
     <course-name>Physics for Poets</course-name>
     <grade>A</grade>
     <credits>10</credits>
   </course>
   <course>
     <course-name>Poetry for Physicists</course-name>
     <grade>C+</grade>
     <credits>5</credits>
   </course>
 </term>
 <summary>summary</summary>
 <comments>
    
   comments
 </comments>

</transcript> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

 <xsl:template match="transcript">
   <HTML><BODY>
   <xsl:apply-templates select="student" />

<xsl:for-each select="term/course"> <xsl:sort select="credits" data-type="number" /> <xsl:apply-templates select="." /> </xsl:for-each>
Course Name Grade Credits
   </BODY></HTML>
 </xsl:template>
 <xsl:template match="student">
   Student Transcript<P/>
   Name: 
     <xsl:value-of select="@name" />
     
ID: <xsl:value-of select="@id" />
<P/> </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

Name: name 1
ID: STU12345


Course Name Grade Credits
course 2 B+ 3
course 1 A- 4
Poetry for Physicists C+ 5
Physics for Poets A 10
  </BODY>

</HTML>

</source>
   
  


Format output with HTML tags

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <itinerary>

 <day number="1">day 1</day>
 <day number="2">day 2</day>
 <day number="3">day 3</day>
 <day number="4">day 4</day>
 <day number="5">day 5</day>
 <day number="6">day 6</day>
 <day number="7">day 7</day>
 <day number="8">day 8</day>
 <day number="9">day 9</day>

</itinerary>

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

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <html>
     <head>
       <title>Itinerary</title>
     </head>
     <body>
         <xsl:apply-templates select="//day" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="day">

Day <xsl:value-of select="@number" />

<xsl:apply-templates />

 </xsl:template>

</xsl:stylesheet> Output: <html>

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

Day 1

day 1

Day 2

day 2

Day 3

day 3

Day 4

day 4

Day 5

day 5

Day 6

day 6

Day 7

day 7

Day 8

day 8

Day 9

day 9

  </body>

</html>

</source>
   
  


html output method to make br tags come out as

   <source lang="xml">

File: Data.xml <wine grape="Cabernet">

 <winery>shop 1</winery>
 <product>product 1</product>
 <year>1996</year>
 <price>11.99</price>

</wine> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="html" />
 <xsl:template match="winery">
   
     
       <xsl:apply-templates />
       <xsl:text> </xsl:text>
       <xsl:value-of select="../@grape" />
     
   
   
</xsl:template> <xsl:template match="product"> <xsl:apply-templates />
</xsl:template> <xsl:template match="year | price"> <xsl:apply-templates />
</xsl:template>

</xsl:stylesheet> Output:

 shop 1 Cabernet
product 1
1996
11.99
</source>


One html tag per template

   <source lang="xml">

File: Data.xml

File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:template match="emphasis">
   
     <xsl:apply-templates />
   
 </xsl:template>
 <xsl:template match="literal">
   
     <xsl:apply-templates />
   
 </xsl:template>
 <xsl:template match="chapter">
   <html>
     <xsl:apply-templates />
   </html>
 </xsl:template>
 <xsl:template match="para">

<xsl:apply-templates />

 </xsl:template>
 <xsl:template match="chapter/title">

<xsl:apply-templates />

 </xsl:template>

</xsl:stylesheet>

</source>
   
  


Output html img tag

   <source lang="xml">

File: Data.xml

<xdata> </xdata>

File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" />
 <xsl:template match="xdata">
   <section>
     <xsl:element name="author">
       <xsl:attribute namespace="http://www.w3.org/1999/xlink"
         name="type">simple</xsl:attribute>
       <xsl:attribute namespace="http://www.w3.org/1999/xlink"
         name="href">a.html</xsl:attribute>
     </xsl:element>
     <xsl:apply-templates />
   </section>
 </xsl:template>

</xsl:stylesheet> Output:


<section><author xmlns:ns0="http://www.w3.org/1999/xlink" ns0:type="simple" ns0:href="a.html"/> </section>

</source>
   
  


Output html with frameset

   <source lang="xml">

File: Data.xml <?xml version = "1.0" encoding = "UTF-8"?> <employees>

 <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>
 <employee eid="2" dept="training">
   <contact addInfo="info2">
     <name>
       <firstName>Sam</firstName>
       <middleName int="S">Stolte</middleName>
       <lastName>Williams</lastName>
     </name>
     <address>
       <street>1 St.</street>
       <city>Austin</city>
       <state>Texas</state>
       <zipcode>22222</zipcode>
     </address>
     <phone>
       <tel type="wk">512-3467899</tel>
       <tel type="hm">512-4623356</tel>
       <fax>512-3465655</fax>
     </phone>
     <email>s@s.ru</email>
   </contact>
   <hireDate>2000-03-11</hireDate>
 </employee>

</employees> 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>Frame Document</title>
     </head>
     <frameset cols="30%,*">
       <frame src="nav.html" />
       <xsl:document href="nav.html">
         <html>
           <head>
             <title>Navigation</title>
           </head>
           <body>
             <xsl:apply-templates mode="nav" select="*" />
           </body>
         </html>
       </xsl:document>
       <frame src="body.html" />
       <xsl:document href="body.html">
         <html>
           <head>
             <title>Email Listing</title>
           </head>
           <body>
             <xsl:apply-templates select="*" />
           </body>
         </html>
       </xsl:document>
     </frameset>
   </html>
 </xsl:template>

</xsl:stylesheet>

</source>
   
  


Output various html tags

   <source lang="xml">

File: Data.xml <poem>

 <title>From Book I</title>
 <excerpt>
   <verse>para1</verse>
   <verse>para2</verse>
   <verse>line 2;</verse>
 </excerpt>
 <excerpt>
   <verse>line 2</verse>
   <verse></verse>
 </excerpt>

</poem> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="html" />
 <xsl:template match="poem">
   <html>
     <body>
       <xsl:apply-templates />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="title">

<xsl:apply-templates />

 </xsl:template>
 <xsl:template match="excerpt">

<xsl:apply-templates />


</hr>
 </xsl:template>
 <xsl:template match="verse">
   <xsl:apply-templates />
   
</xsl:template>

</xsl:stylesheet> Output: <html>

  <body>
       

From Book I

para1
para2
line 2;


line 2


  </body>

</html>

</source>
   
  


Output whole xhtml document

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <message>

 You can add processing instructions to a document with the
 <courier>processing-instruction</courier>
 element.

</message> File: Transform.xslt <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" indent="yes" />
 <xsl:template match="/">
   <html>
     <head>
       <title>HTML Output</title>
     </head>
     <body>

<xsl:apply-templates />

     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>HTML Output</title>
  </head>
  <body>

You can add processing instructions to a document with the processing-instruction element.

  </body>

</html>

</source>
   
  


Transformation of book information into XHTML with sorting

   <source lang="xml">

File: Data.xml <?xml version = "1.0" encoding = "UTF-8"?> <book isbn="999-99999-9-X">

 <title>XML Primer</title>
 <author>
   <firstName>A</firstName>
   <lastName>B</lastName>
 </author>
 <chapters>
   <frontMatter>
     <preface pages="2" />
     <contents pages="5" />
     <illustrations pages="4" />
   </frontMatter>
   <chapter number="3" pages="44">Advanced XML</chapter>
   <chapter number="2" pages="35">Intermediate XML</chapter>
   <appendix number="B" pages="26">Parsers and Tools</appendix>
   <appendix number="A" pages="7">Entities</appendix>
   <chapter number="1" pages="28">XML Fundamentals</chapter>
 </chapters>
 <media type="CD" />

</book> File: Transform.xslt <?xml version = "1.0" encoding = "UTF-8"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.w3.org/1999/xhtml">
 
 <xsl:template match="/book">
   ISBN:<xsl:value-of select="@isbn" />
   Title:<xsl:value-of select="title" />
   by<xsl:value-of select="author/lastName"/>,<xsl:value-of select="author/firstName" />
   <xsl:for-each select="chapters/frontMatter/*">
     <xsl:value-of select="name()" />(<xsl:value-of select="@pages" />pages )
   </xsl:for-each>
   <xsl:for-each select="chapters/chapter">
     <xsl:sort select="@number" data-type="number" order="ascending" />
       Chapter: <xsl:value-of select="@number" />(<xsl:value-of select="@pages" />pages )
   </xsl:for-each>
   <xsl:for-each select="chapters/appendix">
     <xsl:sort select="@number" data-type="text" order="ascending" />
               Appendix<xsl:value-of select="@number" />(<xsl:value-of select="@pages" />pages )
   </xsl:for-each>

Pages: <xsl:variable name="pagecount" select="sum(chapters//*/@pages)" /> <xsl:value-of select="$pagecount" />
Media Type: <xsl:value-of select="media/@type" />

 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

   ISBN:999-99999-9-X
   Title:XML Primer
   byB,Apreface(2pages )
   contents(5pages )
   illustrations(4pages )
   
       Chapter: 1(28pages )
   
       Chapter: 2(35pages )
   
       Chapter: 3(44pages )
   
               AppendixA(7pages )
   
               AppendixB(26pages )

Pages: 151
Media Type: CD

</source>
   
  


Use blockquote to output value from xml

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <InvList xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="Schema.xsd">
 <ItemInfo LastUpdated="2005-02-01">
   <ItemName>name 1</ItemName>
   <ItemNum>0001</ItemNum>
   <ItemDesc>description 1</ItemDesc>
   <ItemCost>14.55</ItemCost>
   <ItemLocation>Chicago</ItemLocation>
   <NumInStock>2345</NumInStock>
 </ItemInfo>
 <ItemInfo LastUpdated="2004-12-09">
   <ItemName>name 1</ItemName>
   <ItemNum>0002</ItemNum>
   <ItemDesc>description 2</ItemDesc>
   <ItemCost>9.06</ItemCost>
   <ItemLocation>SanDiego</ItemLocation>
   <NumInStock>13</NumInStock>
 </ItemInfo>
 <ItemInfo LastUpdated="2003-13-19">
   <ItemName>name 1</ItemName>
   <ItemNum>0003</ItemNum>
   <ItemDesc>description 3</ItemDesc>
   <ItemCost>12.34</ItemCost>
   <ItemLocation>Over-seas</ItemLocation>
   <NumInStock>40325</NumInStock>
 </ItemInfo>

</InvList> File: Transform.xslt <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns="http://www.w3.org/TR/REC-html40" xmlns:s="D:\Osborn-McGraw\XML-ComRef\Chapter27"> <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="no" media-type="text/html" /> <xsl:template match="InvList">

 <html>
   <head>
   <title> Inventory List </title>
       <style type="text/css">
       @page {
    margin-left : 15px;
    margin-bottom : 30px;
   margin-right : 15px;
   }
       h1 {
   font-family : Verdana, Arial, sans-serif ;
   font-size : larger;
   background-color : yellow;
   border-bottom-style : double;
   color : Black;
   }
 </style>
 </head>
 <body>
               <xsl:for-each select="ItemInfo">

<xsl:value-of select="ItemName" />

Item Number: <xsl:value-of select="ItemNum" />
Item Description: <xsl:value-of select="ItemDesc" />
Item Cost: $<xsl:value-of select="ItemCost" />
Item Location: <xsl:value-of select="ItemLocation" />
Num. In Stock: <xsl:value-of select="NumInStock" />

               </xsl:for-each>
 </body>
 </html>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><html xmlns:s="D:\Osborn-McGraw\XML-ComRef\Chapter27" xmlns="http://www.w3.org/TR/REC-html40" xmlns:fo="http://www.w3.org/1999/XSL/Format"><head><title> Inventory List </title><style type="text/css">

       @page {
    margin-left : 15px;
    margin-bottom : 30px;
   margin-right : 15px;
   }
       h1 {
   font-family : Verdana, Arial, sans-serif ;
   font-size : larger;
   background-color : yellow;
   border-bottom-style : double;
   color : Black;
   }
</style></head><body>

name 1

Item Number: 0001
Item Description: description 1
Item Cost: $14.55
Item Location: Chicago

Num. In Stock: 2345

name 1

Item Number: 0002
Item Description: description 2
Item Cost: $9.06
Item Location: SanDiego

Num. In Stock: 13

name 1

Item Number: 0003
Item Description: description 3
Item Cost: $12.34
Item Location: Over-seas

Num. In Stock: 40325
</body></html>
</source>
   
  


Wrap HTML tags in template

   <source lang="xml">

File: Data.xml

<poem year="1667" type="epic">

 <verse>line 3</verse>
 <verse>line 4</verse>

</poem> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="verse">
   <html>
     <body>
       <xsl:apply-templates />
     </body>
   </html>
 </xsl:template>
 

</xsl:stylesheet> Output:

 <html><body>line 3</body></html>
 <html><body>line 4</body></html>
</source>