XML教程
XQuery教程
XSLT教程
XPath教程

XSLT 语法

XSLT语法

让我们以一个示例来获取示例XML文件并将其转换为格式正确的HTML文档。
请参见此示例:
创建一个名为employee.xml的XML文件,其具有以下代码:
Employee.xml
<?xml version = "1.0"?>
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>
为上述XML文档定义XSLT样式表文档。您应遵循以下条件:
页面应具有标题雇员。 页面应具有员工详细信息表。 列中应包含以下标题: id,名字,姓氏,昵称,薪金 表中必须包含相应的员工详细信息。
步骤1: 创建XSLT文档
创建满足上述要求的XSLT文档。将其命名为employee.xsl并将其保存在employee.xml的相同位置。
Employee.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>Employee</h2> 
            <table border = "1"> 
               <tr bgcolor = "#9acd32"> 
                  <th>ID</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Salary</th> 
               </tr> 
               <!--for-each processing instruction 
               Looks for each element matching the XPath expression 
              --> 
              <xsl:for-each select="class/employee"> 
                  <tr> 
                     <td> 
                        <!--value-of processing instruction 
                        process the value of the element matching the XPath expression 
                       --> 
                        <xsl:value-of select = "@id"/> 
                     </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 = "salary"/></td>   
                  </tr> 
               </xsl:for-each> 
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>
步骤2: 使用以下xml-stylesheet标记将XSLT文档列出到XML文档
更新employee.xml文档。将href值设置为employee.xsl
<?xml version = "1.0"?> 
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
... 
</class>
更新了" employee.xml"
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>
第3步: 在Internet Explorer中查看XML文档
输出将如下所示:
输出:
XSLT语法1
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4