XSLT <import>標籤


<xsl:import>標記將一個樣式表的內容匯入另一個樣式表。 匯入樣式表優先於匯入的樣式表。

宣告

以下是<xsl:import>元素的語法宣告。

<xsl:import href = "uri"> 
</xsl:import>

屬性

  • href - 用於傳遞要匯入的xslt樣式表的路徑

元素

  • 匹配次數 - 不限
  • 父元素 - xsl:stylesheet, xsl:transform
  • 子元素 - None

範例

此範例通過疊代每個學生建立一個<student>元素列表及其屬性rollno,及子素:<firstname><lastname><nickname><marks>。 在這裡,我們建立了兩個xsl樣式表,其中檔案:students_imports.xsl 樣式表中匯入了檔案:students.xslstudents.xml 連結到students_imports.xsl

檔案:students.xml -

<?xml version = "1.0"?> 
<?xml-stylesheet type = "text/xsl" href = "students_imports.xsl"?> 
<class> 
   <student rollno = "10010">
      <firstname>Machine</firstname> 
      <lastname>Lee</lastname> 
      <nickname>Machine</nickname> 
      <marks>85</marks>
   </student> 
   <student rollno = "10020"> 
      <firstname>Max</firstname> 
      <lastname>Su</lastname> 
      <nickname>Maxsu</nickname> 
      <marks>95</marks>
   </student> 
   <student rollno = "10030"> 
      <firstname>Yii</firstname> 
      <lastname>Bai</lastname> 
      <nickname>Yiibai</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> 
            <h2>學生資訊列表</h2> 
            <table border = "1"> 
               <tr bgcolor = "#12aa32"> 
                  <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> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

檔案:students_imports.xsl -

<?xml version = "1.0" encoding = "UTF-8"?> 
<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
   <xsl:import href = "students.xsl"/>  
   <xsl:template match = "/"> 
      <xsl:apply-imports/> 
   </xsl:template>  
</xsl:stylesheet>

在瀏覽器中打上面檔案:student.xml,效果如下所示 -