文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 大熊 [2004-10-18 00:29:30]

1. 描述

12.5 Transforming an XML Document Using XSLT Credit: David Ascher

12.5 使用XSLT来转换XML文档

感谢:David Ascher

1.1. 问题 Problem

12.5.1 Problem You have an XSLT transform that you wish to programmatically run through XML documents.

12.5.1 问题

你有一个XSLT转换,希望通过编程来使它作用于XML文档。

1.2. 解决 Solution

12.5.2 Solution The solution depends on the XSLT processor you're using. If you're using Microsoft's XSLT engine (part of its XML Core Services package), you can drive the XSLT engine through its COM interface:

12.5.2 解决

本解决方案依赖于你所使用的XSLT处理器。如果你使用的是Microsoft的XSLT引擎(作为XML Core Servirces包中的一部分),你可以通过它的COM接口来驱动XSLT引擎:

   1 def process_with_msxslt(xsltfname, xmlfname, targetfname):
   2     import win32com.client.dynamic
   3     xslt = win32com.client.dynamic.Dispatch("Msxml2.DOMDocument.4.0")
   4     xslt.async = 0
   5     xslt.load(xsltfname)
   6     xml = win32com.client.dynamic.Dispatch("Msxml2.DOMDocument.4.0")
   7     xml.async = 0
   8     xml.load(xmlfname)
   9     output = xml.transformNode(xslt)
  10     open(targetfname, 'wb').write(output)

If you'd rather use Xalan's XSLT engine, it's as simple as using the right module:

如果你更喜欢用Xalan的XSLT引擎,它的使用如下面代码所示般简单:

   1 import Pyana
   2 output = Pyana.transform2String(source=open(xmlfname).read(  ),
   3                                 style=open(xsltfname).read(  ))
   4 open(targetfname, 'wb').write(output)

1.3. 讨论 Discussion

12.5.3 Discussion There are many different ways that XML documents need to be processed. Extensible Stylesheet Language Transformations (XSLT) is a language that was developed specifically for transforming XML documents. Using XSLT, you define a stylesheet, which is a set of templates and rules that defines how to transform specific parts of an XML document into arbitrary outputs.

12.5.3 讨论

有许多不同的方法来处理XML文档。Extensible Stylesheet Language Transformations(XSLT,扩展样式表语言转换)是一种特别为转换XML文档开发的语言。使用XSLT,你需要定义一个样式表,也就是一套模板和规则,定义了如何来转换一个XML文档中的特定部分为任意输出。

The XSLT specification is a World Wide Web Consortium Recommendation (http://www.w3.org/TR/xslt) that has been implemented by many different organizations. The two most commonly used XSLT processors are Microsoft's XLST engine, part of its XML Core Services, and the Apache group's Xalan engines (C++ and Java versions are available).

XSLT规范是由WWW联盟推荐的( http://www.w3.org/TR/xslt ),许多组织也已经实现了它。两个最为通用的XSLT处理器是Microsoft的XSLT引擎,XML Core Services中的一部分,以及Apache组织的Xalan引擎(有C++和Java两个版本可用)。

If you have an existing XSLT transform, running it from Python is easy with this recipe. The first variant uses the COM interface (provided automatically by the win32com package, part of win32all) to Microsoft's engine, while the second uses Brian Quinlan's convenient wrapper around Xalan, Pyana (http://pyana.sourceforge.net/). While this recipe shows only the easiest way of using Xalan through Pyana, there's a lot more to the package. You can easily extend XSLT and XPath with Python code, something which can save you a lot of time if you know Python well.

如果你有一个XSLT转换,参照本处方,通过python来运行也是很容易的。第一种形式里,使用COM接口(由win32com包自动提供,win32all的一部分)来获得Microsoft的引擎,而第二种是使用Brian Quinlan的方便的Xalan包装,Pyana( http://pyana.sourceforge.net )。本处方中仅显示了通过Pyana来使用的Xalan的最为简单的方式,该能用来完成更多的事。你可以容易使用Python代码来延伸XSLT和XPath,如果你熟悉Python的话,那它有时可以帮你节省许多的时间。

XSLT is definitely trendy, partially because it seems at first well-suited to processing XML documents. If you're comfortable with XSLT and want to use Python to help you work with your existing stylesheets, these recipes will start you on your way. If, however, you're quite comfortable with Python and are just starting with XSLT, you may find that it's easier to forget about these newfangled technologies and use good old Python code to do the job. See Recipe 12.6 for a very different approach.

XSLT很流行,部分是因为它看上去是很适合处理XML文档。如果你对使用XSLT感到很得心应手,希望使用Python来帮助你在工作中使用现有的样式表,这些处方将是一个开端。然而如果你相当熟悉Python,正准备开始使用XSLT,你可能发现它是较为容易忘记的那些新奇的技术之一,会使用老的同样能完成任务的Python代码来完成你的工作。处方12.6中就演示的一个相当不同的处理方式。

1.4. 参考 See Also

12.5.4 See Also Recipe 12.6 for a pure Python approach to the same problem; Recipe 12.10; Pyana is available and documented at http://pyana.sourceforge.net; Apache's Xalan is available and documented at http://xml.apache.org; Microsoft's XML technologies are available from Microsoft's developer site (http://msdn.microsoft.com).

12.5.4 参考

处方12.6演示了一种纯的Python代码来解决同样的问题;处方12.10;Pyana以及相关文档可在 http://pyana.sourceforge.net 上获得;Apache的Xalan及其文档可在 http://xml.apache.org 上获得;Microsoft的XML技术可在Microsoft的开发者网站( http://msdn.microsoft.com )上获得。