- XSLT 教程
- XSLT - 主页
- XSLT - 概览
- XSLT - 语法
- XSLT - <template>
- XSLT - <value-of>
- XSLT - <for-each>
- XSLT - <sort>
- XSLT - <if>
- XSLT - <choose>
- XSLT - <key>
- XSLT - <message>
- XSLT - <apply-template>
- XSLT - <import>
- XSLT 有用的资源
- XSLT - 快速指南
- XSLT - 有用的资源
- XSLT - 讨论
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 文档。将 href 值设置为 students.xsl
<?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>
输出
广告