XML Tutorial/XSLT stylesheet/union

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

Union matching

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<provinces>
 <province id="AB">
  <name>Alberta</name>
  <abbreviation>AB</abbreviation>
 </province>
 <province id="BC">
  <name>British Columbia</name>
  <abbreviation>BC</abbreviation>
 </province>
</provinces>
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:output doctype-system="http://www.w3.org/TR/html4/strict.dtd" />
  <xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN" />
  <xsl:template match="provinces">
    <html>
      <head>
        <title>Provinces of Canada and Abbreviations</title>
      </head>
      <body>
        <h3>
          Provinces of Canada and Abbreviations
        </h3>
        <table>
          <thead>
            <tr>
              <td>Province</th>
              <td>Abbreviation</th>
            </tr>
          </thead>
          <tbody align="center">
            <xsl:apply-templates select="province" />
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="province">
    <tr>
      <xsl:apply-templates select="name|abbreviation" />
    </tr>
  </xsl:template>
  <xsl:template match="name|abbreviation">
    <td>
      <xsl:apply-templates />
    </td>
  </xsl:template>
</xsl:stylesheet>
Output:
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Provinces of Canada and Abbreviations</title>
   </head>
   <body>
      <h3>
                   Provinces of Canada and Abbreviations
                 
      </h3>
      <table>
         <thead>
            <tr>
               <td>Province</th>
               <td>Abbreviation</th>
            </tr>
         </thead>
         <tbody align="center">
            <tr>
               <td>Alberta</td>
               <td>AB</td>
            </tr>
            <tr>
               <td>British Columbia</td>
               <td>BC</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>