Skip to content

XML XSLT

XSLT(可扩展样式表语言转换,Extensible Stylesheet Language Transformations)是一种专门用于将 XML 文档转换为其他格式(例如 HTML、纯文本,甚至其他 XML 结构)的强大语言。

虽然 CSS 可以直接在浏览器中为 XML 设置样式以便显示(参阅“显示 XML”章节),但 XSLT 对输出结构提供了更多的控制:

  • 结构转换: 添加、删除、重新排序或重命名元素和属性。
  • 数据操作: 排序数据、执行条件处理(if/then 逻辑)、分组元素。
  • 内容生成: 基于 XML 源创建新内容。
  • 格式转换: 将 XML 转换为 HTML、XHTML、文本、CSV、JSON(使用 XSLT 3.0)或其他 XML 词汇表。
  • 可重用性: 定义处理特定元素的模板。

XSLT 使用 XPath 表达式从输入的 XML 文档中选择需要处理的节点(元素、属性、文本)。

XSLT 有多个版本(1.0、2.0、3.0),每个版本都增加了更多功能。XSLT 1.0 被广泛支持,而 2.0 和 3.0 提供了显著的增强,特别是在数据操作和输出格式方面。浏览器支持通常仅限于 XSLT 1.0。

了解更多关于 XSLT 的信息

XSLT 示例:将早餐菜单转换为 HTML

Section titled “XSLT 示例:将早餐菜单转换为 HTML”

考虑以下 XML 文档(breakfast.xml):

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food available="true">
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food available="true">
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food available="false"> <!-- Example of unavailable item -->
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food available="true">
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
</breakfast_menu>

我们可以使用以下 XSLT 样式表(breakfast_transform.xsl)将此 XML 转换为 HTML 页面,仅显示可用的项目:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Breakfast Menu</title>
<style>
body { font-family: Arial, sans-serif; font-size: 12pt; background-color: #EEEEEE; }
.item { margin-bottom: 1em; border: 1px solid #ccc; background-color: white; padding: 5px; }
.item-header { background-color: teal; color: white; padding: 4px; font-size: 11pt; }
.item-name { font-weight: bold; }
.item-details { margin-left: 20px; margin-bottom: 1em; font-size: 10pt; }
.calories { font-style: italic; color: #555; }
</style>
</head>
<body>
<h1>Today's Breakfast Menu</h1>
<!-- Apply templates to process only 'food' elements where @available='true' -->
<xsl:apply-templates select="breakfast_menu/food[@available='true']"/>
</body>
</html>
</xsl:template>
<!-- Template to process each selected 'food' element -->
<xsl:template match="food">
<div class="item">
<div class="item-header">
<span class="item-name"><xsl:value-of select="name"/></span> -
<span><xsl:value-of select="price"/></span>
</div>
<div class="item-details">
<p><xsl:value-of select="description"/></p>
<p class="calories">(<xsl:value-of select="calories"/> calories per serving)</p>
</div>
</div>
</xsl:template>
</xsl:stylesheet>

解释:

  • <?xml-stylesheet ...?>:此处理指令将 XSLT 文件链接到 XML 文件,以便进行客户端转换(参阅“显示 XML”章节)。
  • <xsl:stylesheet>:将文档定义为 XSLT 样式表。
  • <xsl:output method="html">:指定输出应为 HTML。
  • <xsl:template match="/">:匹配 XML 文档的根节点,并定义基本的 HTML 结构。
  • <xsl:apply-templates select="breakfast_menu/food[@available='true']"/>:这是关键。它仅选择是 breakfast_menu 的子元素并且具有属性 available 值为 'true' 的 food 元素。然后它指示处理器为这些选定的节点查找匹配的模板。
  • <xsl:template match="food">:此模板匹配由 apply-templates 选择的每个 food 元素。
  • <xsl:value-of select="..."/>:提取选定子元素(例如,name、price)的文本内容。
  • 生成的 HTML 使用 CSS 类进行样式设置,这通常比内联样式更佳。

此转换可以在服务器上进行,然后再将 HTML 发送给浏览器,或者如果浏览器支持 XSLT 1.0 并且 XML 文档包含 <?xml-stylesheet ...?> 处理指令,也可以直接在浏览器中进行。