xslt note - 2

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">(取得root element)
     <html>
          <xsl:apply-templates />
    </html>
</xsl:template>

<xsl:template match="book">(從match範圍找名子為book的element)
<head>
<title>
  ISBN
  <xsl:value-of select="@isbn" /> (取得屬性值)
  
  <xsl:value-of select="title" /> (取得element值)
  </title>
  </head>
<body bgcolor="white">
<h1>
  <xsl:value-of select="title" />
  </h1>
<h2>
  by
  <xsl:value-of select="author/lastName" /> (取得author/lastName的element值)
  ,
  <xsl:value-of select="author/firstName" />
  </h2>
<table border="1">
<xsl:for-each select="chapters/preface">
  <xsl:sort select="@num" order="ascending" /> (用num的vlalue來排序 以ascending順序)
<tr>
<td align="right">
  Preface
  <xsl:value-of select="@num" />
  </td>
<td>
  <xsl:value-of select="." /> (.為目前element的名子 用法類似xpath)
  (   <xsl:value-of select="@pages" />   pages )
  </td>
  </tr>


原始檔案
<?xml version = "1.0"?>

<!-- Fig. 12.7 : usage.xml            -->
<!-- Usage of elements and attributes -->

<?xml:stylesheet type = "text/xsl" href = "usage.xsl"?>

<book isbn = "999-99999-9-X">
   <title>Deitel&apos;s XML Primer</title>

   <author>
      <firstName>Paul</firstName>
      <lastName>Deitel</lastName>
   </author>

   <chapters>
      <preface num = "1" pages = "2">Welcome</preface>
      <chapter num = "1" pages = "4">Easy XML</chapter>
      <chapter num = "2" pages = "2">XML Elements?</chapter>
      <appendix num = "1" pages = "9">Entities</appendix>
   </chapters>

   <media type = "CD"/>
</book>

轉成檔案
<html> 
   <head>
     <title> ISBN 999-99999-9-X Deitel&apos;s XML Primer </title>
  </head>

   <body bgcolor="white">
     <h1> Deitel&apos;s XML Primer </h1>
     <h2> by Deitel , Paul  </h2>
     <table border="1">
     <tr>
         <td align="right">  Preface 1  </td>
         <td> Welcome  (2 pages ) </td>
    </tr>
 </body>
</html>