Xslt-syntax

提供:Dev Guides
移動先:案内検索

XSLT構文

次のサンプルXMLファイル、students.xmlがあり、適切にフォーマットされたHTMLドキュメントに変換する必要があるとします。

*students.xml*
<?xml version = "1.0"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>
      <marks>85</marks>
   </student>
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>Vinni</nickname>
      <marks>95</marks>
   </student>
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Singh</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

次の基準を満たすために、上記のXMLドキュメントのXSLTスタイルシートドキュメントを定義する必要があります-

  • ページには*生徒*というタイトルが必要です。
  • ページには、学生の詳細の表が必要です。
  • 列には次のヘッダーが必要です:ロール番号、名、姓、ニックネーム、マーク
  • テーブルには、それに応じて学生の詳細が含まれている必要があります。

ステップ1:XSLTドキュメントを作成する

上記の要件を満たすXSLTドキュメントを作成し、students.xslという名前を付けて、students.xmlと同じ場所に保存します。

*students.xsl*
<?xml version = "1.0" encoding = "UTF-8"?>
<!-- xsl stylesheet declaration with xsl namespace:
Namespace tells the xlst processor about which
element is to be processed and which is used for output purpose only
-->
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<!-- xsl template declaration:
template tells the xlst processor about the section of xml
document which is to be formatted. It takes an XPath expression.
In our case, it is matching document root element and will
tell processor to process the entire document with this template.
-->
   <xsl:template match = "/">
      <!-- HTML tags
         Used for formatting purpose. Processor will skip them and browser
            will simply render them.
      -->

      <html>
         <body>
            <h2>Students</h2>

            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>Roll No</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Nick Name</th>
                  <th>Marks</th>
               </tr>

               <!-- for-each processing instruction
               Looks for each element matching the XPath expression
               -->

               <xsl:for-each select="class/student">
                  <tr>
                     <td>
                        <!-- value-of processing instruction
                        process the value of the element matching the XPath expression
                        -->
                        <xsl:value-of select = "@rollno"/>
                     </td>

                     <td><xsl:value-of select = "firstname"/></td>
                     <td><xsl:value-of select = "lastname"/></td>
                     <td><xsl:value-of select = "nickname"/></td>
                     <td><xsl:value-of select = "marks"/></td>

                  </tr>
               </xsl:for-each>

            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

ステップ2:XSLTドキュメントをXMLドキュメントにリンクする

次のxml-stylesheetタグでstudent.xmlドキュメントを更新します。 student.xslにhref値を設定します

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
...
</class>

手順3:Internet ExplorerでXMLドキュメントを表示する

*students.xml*
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>
      <marks>85</marks>
   </student>
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>Vinni</nickname>
      <marks>95</marks>
   </student>
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Singh</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

出力

フォーマットされた出力