XML XSLT

创建于 2024-12-03 / 30
字体: [默认] [大] [更大]

通过使用 XSLT,您可以把 XML 文档转换成 HTML 格式。


使用 XSLT 显示 XML

XSLT 是首选的 XML 样式表语言。

XSLT 比 CSS 复杂得多。使用 XSLT,您可以在输出文件中添加/删除元素和属性。您还可以重新排列和排序元素,执行测试,决定隐藏和显示哪些元素,等等。

XSLT 使用 XPath 在 XML 文档中查找信息。


XSLT 实例

我们将使用以下XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>

<food>
<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>
<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>
<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>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>

</breakfast_menu>
Advertisement

-->

XSLT 是在浏览器显示 XML 文件之前,先把它转换为 HTML:

XSLT样式表实例:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="description"/>
    <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html> Transform the XML Document with XSLT »

XSLT 教程

如果您想要学习有关 XSLT 的知识,请访问 XSLT 教程



0 人点赞过