用于数据的 xml: xsl 样式表:推还是拉?
英文原文
内容:
推样式表
拉样式表
因此,拉总是比推好,对吗?
结束语
参考资料
关于作者
对本文的评价
相关内容:
订阅 developerworks 时事通讯
在 xml 专区还有:
教程
工具和产品
代码与组件
文章
查看两种 xsl 样式表的制作技术以及如何将其综合用于数据
kevin williams(kevin@blueoxide.com)
ceo,blue oxide technologies,llc
2002 年 5 月
专栏作家 kevin williams 研究了用于创建 xsl 样式表的两种最常见的制作样式:推(push)和拉(pull)。他研究了一些简单的 xml 和 xsl 示例,并讨论了每种方法的优缺点。
当制作 xsl 样式表时,可以使用这两种主要制作样式中的任何一种:
推 样式是由在输出文档中创建输出的处理程序构造的。输出基于源文档中遇到的元素类型。元素内容被推 给适当的处理程序。
另一方面,拉 样式根据需要通过拉出 源文档中的内容来构建输出文档。
您所选择的样式对样式表的代码复杂性和可维护性具有重要的影响。
推样式表
推样式表是样式表的经典形式。所有主要的 xsl 生成工具都可以生成推样式表,它们是 xsl 参考资料中通常讨论的形式。这种格式的样式表由一系列 xsl:template 元素组成,每个元素处理文档中的一个特定元素类型。例如,假设您有下列源文档:
清单 1. 样本数据文档
<orders>
<invoice>
<customername>kevin williams</customername>
<address>100 nowhere lane</address>
<city>nowheresville</city>
<state>va</state>
<zip>24182</zip>
<item>
<id>e38-19273</id>
<description>3-inch grommets</description>
<count>37</count>
<totalcost>37.00</totalcost>
</item>
<item>
<id>e22-18272</id>
<description>2-inch widgets</description>
<count>22</count>
<totalcost>11.00</totalcost>
</item>
</invoice>
<invoice>
<customername>john public</customername>
<address>200 anywhere street</address>
<city>anytown</city>
<state>va</state>
<zip>24177</zip>
<item>
<id>e22-18272</id>
<description>2-inch widgets</description>
<count>27</count>
<totalcost>13.50</totalcost>
</item>
<item>
<id>e19-28376</id>
<description>1-inch bolts</description>
<count>16</count>
<totalcost>4.00</totalcost>
</item>
</invoice>
</orders>
现在,假设您要将其转换成下面的 xhtml 文档:
清单 2. 样本数据输出
<html>
<body>
<h1>kevin williams</h1>
<p>100 nowhere lane<br />
nowheresville, va 24182</p>
<table>
<tr>
<th>description</th>
<th>cost</th>
</tr>
<tr>
<td>3-inch grommets</td>
<td>37.00</td>
</tr>
<tr>
<td>2-inch widgets</td>
<td>11.00</td>
</tr>
</table>
<p />
<h1>john public</h1>
<p>200 anywhere street<br />
anytown, va 24177</p>
<table>
<tr>
<th>description</th>
<th>cost</th>
</tr>