copy-of copies everything : copy of « XSLT stylesheet « XML Tutorial






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>








5.47.copy of
5.47.1.
5.47.2.copy-of copies everything
5.47.3.specify element to copy