XML/XSLT stylesheet/apply templates

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

apply template

File: Data.xml
<?xml version="1.0"?>
<people>
  <person born="1912" died="1954">
    <name>
      <first_name>A</first_name>
      <last_name>B</last_name>
    </name>
    <profession>C</profession>
    <profession>D</profession>
    <profession>E</profession>
  </person>
  <person born="2008" died="2008">
    <name>
      <first_name>F</first_name>
      <middle_initial>G</middle_initial>
      <last_name>H</last_name>
    </name>
    <profession>I</profession>
    <hobby>J</hobby>
  </person>
</people>

File: Transform.xslt
<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="name">
    <xsl:value-of select="last_name"/>, 
    <xsl:value-of select="first_name"/>
  </xsl:template>
  <xsl:template match="person">
    <xsl:apply-templates select="name"/>
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?>
  B, 
    A
  H, 
    F



Apply template, select from a list

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>
</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>
      <xsl:apply-templates select="author, title, price" />
    </tr>
  </xsl:template>

  <xsl:template match="author | title | price">
    <td>
      <xsl:value-of select="." />
    </td>
  </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>
         
      </table>
   </body>
</html>



apply-templates select="tag name"

File: Data.xml
<employees>
  <employee hireDate="04/23/1999">
    <last>A</last>
    <first>B</first>
    <salary>1000</salary>
  </employee>
  <employee hireDate="09/01/1998">
    <last>C</last>
    <first>D</first>
    <salary>9500</salary>
  </employee>
  <employee hireDate="08/20/2000">
    <last>F</last>
    <first>G</first>
    <salary>8900</salary>
  </employee>
</employees>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:template match="employees">
    <xsl:apply-templates>
      <xsl:sort select="salary" />
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="employee">
    Last:
    <xsl:apply-templates select="last" />
    First:
    <xsl:apply-templates select="first" />
    Salary:
    <xsl:apply-templates select="salary" />
    Hire Date:
    <xsl:apply-templates select="@hireDate" />
    <xsl:text></xsl:text>
   </xsl:template>
</xsl:stylesheet>

Output:

  
  
  

    Last:
    A
    First:
    B
    Salary:
    1000
    Hire Date:
    04/23/1999
    Last:
    F
    First:
    G
    Salary:
    8900
    Hire Date:
    08/20/2000
    Last:
    C
    First:
    D
    Salary:
    9500
    Hire Date:
    09/01/1998



apply-templates with mode=toc

File: Data.xml
<?xml version="1.0"?>
<people>
  <person born="1912" died="1954">
    <name>
      <first_name>A</first_name>
      <last_name>B</last_name>
    </name>
    <profession>C</profession>
    <profession>D</profession>
    <profession>E</profession>
  </person>
  <person born="2008" died="2008">
    <name>
      <first_name>F</first_name>
      <middle_initial>G</middle_initial>
      <last_name>H</last_name>
    </name>
    <profession>I</profession>
    <hobby>J</hobby>
  </person>
</people>

File: Transform.xslt
<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="people">
    <html>
      <head><title>Famous Scientists</title></head>
      <body>
        <ul><xsl:apply-templates select="person" mode="toc"/></ul>
        <xsl:apply-templates select="person"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="person" mode="toc">
    <xsl:apply-templates select="name" mode="toc"/>
  </xsl:template>
  <xsl:template match="name" mode="toc">
    <li><xsl:value-of select="last_name"/>, 
    <xsl:value-of select="first_name"/></li>
  </xsl:template>
  <xsl:template match="person">
    <p><xsl:apply-templates/></p>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Famous Scientists</title>
   </head>
   <body>
      <ul>
         <li>B, 
            A
         </li>
         <li>H, 
            F
         </li>
      </ul>
      <p>
         
         A
         B
         
         C
         D
         E
         
      </p>
      <p>
         
         F
         G
         H
         
         I
         J
         
      </p>
   </body>
</html>



copy the result of apply-templates

File: Data.xml
<colors>
  <color>
    red:
    <shade>A</shade>
    <shade>B</shade>
    <shade>C</shade>
  </color>
  <color>yellow</color>
  <color>
    blue:
    <shade>F</shade>
    <shade>G</shade>
    <shade>H</shade>
  </color>
</colors>

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="yes" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<colors>
    <color>
    red:
    <shade>A</shade>
        <shade>B</shade>
        <shade>C</shade>
    </color>
    <color>yellow</color>
    <color>
    blue:
    <shade>F</shade>
        <shade>G</shade>
        <shade>H</shade>
    </color>
</colors>