XML Tutorial/XSLT stylesheet/apply templates

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

apply-templates select="county" mode="county"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<us>
 <state name="Hawaii">
  <county name="Hawaii">
   <city class="largest">Hilo</city>
  </county>
 </state>
</us>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:template match="us/state">
    <html>
      <head>
        <title>
          State:
          <xsl:value-of select="@name" />
        </title>
      </head>
      <body>
        <h1>
          State:
          <xsl:value-of select="@name" />
        </h1>
        <h2>All Counties</h2>
        <ul>
          <xsl:apply-templates select="county" mode="county" />
        </ul>
        <h2>Largest Cities (by County)</h2>
        <ul>
          <xsl:apply-templates select="county" mode="city" />
        </ul>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="county" mode="county">
    <li>
      <xsl:value-of select="@name" />
    </li>
  </xsl:template>
  <xsl:template match="county" mode="city">
    <li>
      <xsl:value-of select="city" />
      (
      <xsl:value-of select="@name" />
      )
    </li>
  </xsl:template>
</xsl:stylesheet>
Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>
                   State:
                   Hawaii
      </title></head>
   <body>
      <h1>
                   State:
                   Hawaii
      </h1>
      <h2>All Counties</h2>
      <ul>
         <li>Hawaii</li>
      </ul>
      <h2>Largest Cities (by County)</h2>
      <ul>
         <li>Hilo
                  (
                  Hawaii
                  )
                
         </li>
      </ul>
   </body>
</html>


Creating and Applying Template Rules

File: Data.xml
 
<?xml version="1.0"?>
<employees>
  <animal>
    <name language="English">T1</name>
    <name language="Latin">T2</name>
    <projects>
      <project>project1</project>
    </projects>
  </animal>
</employees>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/">
    <html>
      <head>
        <title>this is the title</title>
      </head>
      <body bgcolor="white">
        <xsl:apply-templates select="employees/animal" />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="animal">
    <p align="center">
      <br />
      <font size="+3">
        <xsl:apply-templates select="name" />
      </font>
    </p>
    <paragraph>
      <xsl:value-of select="name[@language="English"]" />
      <a href="http://www.wbex.ru">pages</a>
    </p>
    <hr />
  </xsl:template>
  <xsl:template match="name[@language="English"]">
    <nobr>
      <b>
        <xsl:value-of select="." />
        :
      </b>
    </nobr>
  </xsl:template>
  <xsl:template match="name[@language="Latin"]">
    <nobr>
      <i>
        <xsl:value-of select="." />
      </i>
    </nobr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>this is the title</title>
   </head>
   <body bgcolor="white">
      <p align="center"><br><font size="+3">
            <nobr><b>T1
                          :
                        </b></nobr>
            <nobr><i>T2</i></nobr></font></p>
      <paragraph>T1<a href="http://www.wbex.ru">pages</a></p>
      <hr>
   </body>
</html>


Template rules are modules that describe how a particular part of your source XML should be output

A template rule has three parts: 
    the opening tag describes which part(s) of your XML document the template should be applied to, 
    the middle bit describes what should happen once a match is found, 
    and the closing tag completes the template.

File: Data.xml
<?xml version="1.0"?>
<python version="2.3">
  <keyword>while</keyword>
  <keyword>continue</keyword>
  <keyword>def</keyword>
  <keyword>elif</keyword>
  <keyword>except</keyword>
  <keyword>from</keyword>
</python>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:saxon="http://icl.ru/saxon"
  extension-element-prefixes="saxon">
  <xsl:output method="text" encoding="ISO-8859-1" />
  <xsl:strip-space elements="*" />
  <xsl:template match="python">
    <!-- to result tree as text -->
    <xsl:text>Python 2.3 Keywords&#10;&#10;</xsl:text>
    <xsl:apply-templates select="keyword" mode="text">
      <xsl:sort />
    </xsl:apply-templates>
    <!-- save as HTML, too -->
    <saxon:output href="keywords.html" method="html" indent="yes"
      saxon:indent-spaces="1">
      <xsl:fallback />
      <html>
        <body>
          <h3>Python 2.3 Keywords</h3>
          <ol>
            <xsl:apply-templates select="keyword"
              mode="html">
              <xsl:sort />
            </xsl:apply-templates>
          </ol>
        </body>
      </html>
    </saxon:output>
  </xsl:template>
  <xsl:template match="keyword" mode="html">
    <li>
      <xsl:value-of select="." />
    </li>
  </xsl:template>
  <xsl:template match="keyword" mode="text">
    <xsl:value-of select="." />
    <xsl:choose>
      <xsl:when
        test="not((position() mod 5)=0) and not(position()=last())">
        <xsl:text>&#09;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>&#10;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
Output:
Python 2.3 Keywords
continue  def  elif  except  from
while