在xml这个 系列中
xml用来负责数据的存储。
xslt用来负责数据的修正,塞选和显示
这里我做一个简单的
xml+xsl的关联。
目的:使大家了解xml和xsl是怎么关联
并且影响数据显示的。
xml 和xsl的关联是通过
在xml文档中加入
xml-stylesheet处理指令
<?xml-stylesheet type=”text/xsl” href=”xxxx.xsl”?>
如下面的例子
XML File (prodsort.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="prodsort.xsl" ?>
<products>
<product prodID="AX5608">
<name>FooBar</name>
<version>1.5</version>
<descr>Processes foo objects using standard FB API</descr>
<categ>Software</categ>
<price curr="USD">149.99</price>
<units>each</units>
</product>
<product prodID="CB3241">
<name>TrixelMaker</name>
<version>3.0</version>
<descr>Burns multiple trixels from single master (requires Wooden Rings 1.6 or greater)</descr>
<categ>Hardware</categ>
<price curr="EU">178.49</price>
<units>each</units>
</product>
<product prodID="WD3241">
<name>Widgetry Magick</name>
<version>1.0</version>
<descr>Time-stamps Virtual Widgets</descr>
<categ>Software</categ>
<price curr="GBP">46.80</price>
<units>each</units>
</product>
<product prodID="WR9843">
<name>Wooden Rings Workbench</name>
<version>4.5</version>
<descr>Enhances WRWB 4.0 with "peel'n'play" capability</descr>
<categ>Craftware</categ>
<price curr="USD">129.95</price>
<units>each</units>
</product>
<product prodID="WR9840">
<name>Wooden Rings Workbench</name>
<version>4.0</version>
<descr>Applies Widgetry Magick timestamps to Wooden Rings 1.6 or greater</descr>
<categ>Craftware</categ>
<price curr="USD">109.95</price>
<units>each</units>
</product>
<product prodID="WR1016">
<name>Wooden Rings</name>
<version>1.6</version>
<descr>Our best-selling oaken toroids</descr>
<categ>Craftware</categ>
<price curr="USD">27.99</price>
<units>dozen</units>
</product>
<product prodID="WD">
<name>Virtual Widgets</name>
<version>3.5</version>
<descr>Logs critical events and properties over any object's lifetime</descr>
<categ>Middleware</categ>
<price curr="GBP">109.99</price>
<units>each</units>
</product>
</products>
XSLT File (prodsort.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Wooden Rings and More!</TITLE>
<STYLE type="text/css">
th {background-color: silver;
font-family: Tahoma,Verdana,Arial,sans-serif}
td {background-color: white;
font-family: Tahoma,Verdana,Arial,sans-serif}
</STYLE>
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="products">
<!-- Create a variable to hold the generated result tree fragment. -->
<xsl:variable name="prods_with_usd">
<xsl:apply-templates select="product" mode="calc_usd" />
</xsl:variable>
<TABLE width="75%">
<tr>
<th>Category</th>
<th>Prod ID</th>
<th>Name/Version</th>
<th>Description</th>
<th>Price (Currency)</th>
<th>Price (USD)</th>
</tr>
<!-- Fill remainder of table with source tree "clone". -->
<xsl:apply-templates select="msxsl:node-set($prods_with_usd)/product">
<xsl:sort select="usd_equiv" data-type="number" />
</xsl:apply-templates>
</TABLE>
</xsl:template>
<xsl:template match="product" mode="calc_usd">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:copy-of select="*" />
<!-- Add a calculated <usd_equiv> child to each <product>. -->
<xsl:element name="usd_equiv">
<xsl:choose>
<!-- Note: Exchange rates current as of Oct. 2000. -->
<xsl:when test="price/@curr='USD'">
<xsl:value-of select="format-number(price, '#,##0.00')"/>
</xsl:when>
<xsl:when test="price/@curr='GBP'">
<xsl:value-of select="format-number(price * 1.47275, '#,##0.00')"/>
</xsl:when>
<xsl:when test="price/@curr='EU'">
<xsl:value-of select="format-number(price * 0.864379, '#,##0.00')"/>
</xsl:when>
<xsl:otherwise>Unknown Currency</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:copy>
</xsl:template>
<xsl:template match="product">
<tr>
<td valign="top">
<xsl:value-of select="categ"/>
</td>
<td valign="top">
<xsl:value-of select="@prodID"/>
</td>
<td valign="top">
<xsl:value-of select="concat(name, '/', version)"/>
</td>
<td valign="top">
<xsl:value-of select="descr"/>
</td>
<td valign="top" align="center">
<xsl:value-of select="concat(price, ' (', price/@curr, ')')"/>
</td>
<td valign="top" align="right">
<xsl:value-of select="usd_equiv"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
使用ie访问prodsort.xml文件。就看到最终解析后的结果