XPath元素節點

2019-10-16 23:18:15

獲取和處理元素有多種方法。

第1種方法/class/* - 選擇根節點下的所有元素。

<xsl:for-each select = "/class/*">

第2種方法/class/student - 選擇根節點下的所有student元素。

<xsl:for-each select = "/class/student">

第3種方法//student - 選擇文件中的所有student元素。

<xsl:for-each select = "//student">

範例

在這個例子中,我們建立了一個範例XML文件students.xml,及其樣式表文件students.xsl,它使用了XPath表示式。

以下是使用的範例XML,檔案:students.xml -

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
   <student rollno = "393">
      <firstname>Dinkar</firstname>
      <lastname>Su</lastname>
      <nickname>MaXX</nickname>
      <marks>88</marks>
   </student>
   <student rollno = "493">
      <firstname>Vaneet</firstname>
      <lastname>Lee</lastname>
      <nickname>Vicky</nickname>
      <marks>95</marks>
   </student>
   <student rollno = "593">
      <firstname>Jasvir</firstname>
      <lastname>Wong</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

檔案:students.xsl -

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 

   <xsl:template match = "/">
      <html>
         <body>
            <h3>Details of each Students. Xpath expression = "/class/*"</h3>

            <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>

               <xsl:for-each select = "/class/*">
                  <tr>
                     <td><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> 

            <h3>Details of each Students. Xpath expression = "/class/student"</h3>

            <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>    

               <xsl:for-each select = "/class/student">
                  <tr>
                     <td><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> 

            <h3>Details of each Students. Xpath expression = "//student"</h3>

            <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>    

               <xsl:for-each select = "//student">
                  <tr>
                     <td><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>

開啟瀏覽器,打上面的XML檔案,驗證得到以下結果 -