个性化阅读
专注于IT技术分析

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的相同位置。

员工.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:将XSLT文档列出到XML文档

使用以下xml-stylesheet标签更新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
赞(0)
未经允许不得转载:srcmini » XSLT语法用法示例

评论 抢沙发

评论前必须登录!