XML Tutorial/XSLT stylesheet/copy of

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

copy-of copies everything

File: Data.xml
<?xml version="1.0"?>
<results group="A">
  <match>
    <date>10-Jun-98</date>
    <team score="2">team 1</team>
    <team score="1">team 2</team>
  </match>
  <match>
    <date>10-Jun-98</date>
    <team score="2">team 3</team>
    <team score="2">team 4</team>
  </match>
</results>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:variable name="table-heading">
    <tr>
      <td>
        <b>Date</b>
      </td>
      <td>
        <b>Home Team</b>
      </td>
      <td>
        <b>Away Team</b>
      </td>
      <td>
        <b>Result</b>
      </td>
    </tr>
  </xsl:variable>
  <xsl:template match="/">
    <html>
      <body>
        <h1>
          Matches in Group
          <xsl:value-of select="/*/@group" />
        </h1>
        <xsl:for-each select="//match">
          <h2>
            <xsl:value-of
              select="concat(team[1], " versus ", team[2])" />
          </h2>
          <table bgcolor="#cccccc" border="1"
            cellpadding="5">
            <xsl:copy-of select="$table-heading" />
            <tr>
              <td>
                <xsl:value-of select="date" />
                &#xa0;
              </td>
              <td>
                <xsl:value-of select="team[1]" />
                &#xa0;
              </td>
              <td>
                <xsl:value-of select="team[2]" />
                &#xa0;
              </td>
              <td>
                <xsl:value-of
                  select="concat(team[1]/@score, "-", team[2]/@score)" />
                &#xa0;
              </td>
            </tr>
          </table>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <h1>
                   Matches in Group
                   A
      </h1>
      <h2>team 1 versus team 2</h2>
      <table bgcolor="#cccccc" border="1" cellpadding="5">
         <tr>
            <td><b>Date</b></td>
            <td><b>Home Team</b></td>
            <td><b>Away Team</b></td>
            <td><b>Result</b></td>
         </tr>
         <tr>
            <td>10-Jun-98
                               &nbsp;
                             
            </td>
            <td>team 1
                               &nbsp;
                             
            </td>
            <td>team 2
                               &nbsp;
                             
            </td>
            <td>2-1
                               &nbsp;
                             
            </td>
         </tr>
      </table>
      <h2>team 3 versus team 4</h2>
      <table bgcolor="#cccccc" border="1" cellpadding="5">
         <tr>
            <td><b>Date</b></td>
            <td><b>Home Team</b></td>
            <td><b>Away Team</b></td>
            <td><b>Result</b></td>
         </tr>
         <tr>
            <td>10-Jun-98
                               &nbsp;
                             
            </td>
            <td>team 3
                               &nbsp;
                             
            </td>
            <td>team 4
                               &nbsp;
                             
            </td>
            <td>2-2
                               &nbsp;
                             
            </td>
         </tr>
      </table>
   </body>
</html>


specify element to copy

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <h1>GREETING</h1>
  <paragraph>Hello, world!</p>
</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="/">
      <xsl:apply-templates select="/source/*"/>
    </xsl:template>
    <xsl:template match="h1">
      <xsl:copy use-attribute-sets="H1">
        <xsl:value-of select="."/>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="p">
      <xsl:copy use-attribute-sets="P  ">
        <xsl:value-of select="."/>
      </xsl:copy>
    </xsl:template>
    <xsl:attribute-set name="H1">
      <xsl:attribute name="align">center</xsl:attribute>
      <xsl:attribute name="style">color:red</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set name="P">
      <xsl:attribute name="align">left</xsl:attribute>
      <xsl:attribute name="style">color:blue</xsl:attribute>
    </xsl:attribute-set>
</xsl:stylesheet>


<xsl:copy-of select="."/>

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Bulgaria</state>
  <state>Cyprus</state>
 </candidate>
</eu>

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="xml" />
  <xsl:template match="eu">
    <xsl:apply-templates select="member" />
  </xsl:template>
  <xsl:template match="member">
    <xsl:apply-templates select="state[2]" />
  </xsl:template>
  <xsl:template match="state">
    <xsl:copy-of select="." />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><state founding="yes">Belgium</state>