XML Tutorial/XSLT stylesheet/attribute set — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:26, 26 мая 2010

Several attributes can be grouped together and then used at once with xsl:attribute-set and xsl:use-attribute-sets

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <TITLE>GREETING</TITLE>
  <TEXT>Hello, world!</TEXT>
</data>
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=" TITLE">
      <H1 xsl:use-attribute-sets="titleStyle">
        <xsl:value-of select="."/>
      </H1>
    </xsl:template>
    <xsl:template match=" TEXT">
      <P xsl:use-attribute-sets="textStyle">
        <xsl:value-of select="."/>
      </P>
    </xsl:template>
    <xsl:attribute-set name="titleStyle">
      <xsl:attribute name="align">center</xsl:attribute>
      <xsl:attribute name="style">color:red</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set name="textStyle">
      <xsl:attribute name="align">right</xsl:attribute>
      <xsl:attribute name="style">color:blue</xsl:attribute>
    </xsl:attribute-set>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  <H1 align="center" style="color:red">GREETING</H1>
  <P align="right" style="color:blue">Hello, world!</P>


table with different attribute sets

File: Data.xml
<?xml version="1.0"?>
<!-- albums.xml -->
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>

File: Transform.xslt
<?xml version="1.0"?>
<!-- attribute-set.xsl -->
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:attribute-set name="bold-table">
    <xsl:attribute name="style">
      font-weight: bold; 
    </xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="spacious-table" 
    use-attribute-sets="bold-table">
    <xsl:attribute name="cellpadding">8</xsl:attribute>
    <xsl:attribute name="cellspacing">8</xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="reverse-table"> 
    <xsl:attribute name="bgcolor">black</xsl:attribute>
    <xsl:attribute name="style">color: white;</xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="/list/title"/></title>
      </head>
      <body style="font-family: sans-serif;">
        <xsl:apply-templates select="*"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="list">
    <h1><xsl:value-of select="title"/></h1>
    <table xsl:use-attribute-sets="spacious-table" border="2">
      <xsl:for-each select="listitem">
        <tr>
          <td xsl:use-attribute-sets="reverse-table">
            <xsl:value-of select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
    <h1>Here"s the same table with different attribute sets:</h1>
    <table border="2" xsl:use-attribute-sets="bold-table">
      <xsl:for-each select="listitem">
        <tr>
          <td>
            <xsl:value-of select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>title 1</title>
   </head>
   <body style="font-family: sans-serif;">
      <h1>title 1</h1>
      <table style="&#xA;      font-weight: bold; &#xA;    " cellpadding="8" cellspacing="8" border="2">
         <tr>
            <td bgcolor="black" style="color: white;">item 1</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 2</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 3</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 4</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 5</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 6</td>
         </tr>
         <tr>
            <td bgcolor="black" style="color: white;">item 7</td>
         </tr>
      </table>
      <h1>Here"s the same table with different attribute sets:</h1>
      <table style="&#xA;      font-weight: bold; &#xA;    " border="2">
         <tr>
            <td>item 1</td>
         </tr>
         <tr>
            <td>item 2</td>
         </tr>
         <tr>
            <td>item 3</td>
         </tr>
         <tr>
            <td>item 4</td>
         </tr>
         <tr>
            <td>item 5</td>
         </tr>
         <tr>
            <td>item 6</td>
         </tr>
         <tr>
            <td>item 7</td>
         </tr>
      </table>
   </body>
</html>