XSLT 转换

Example study: How to transform XML into XHTML using XSLT.
实例学习:如何通过XSLT把XML转换成XHTML。

The details of this example will be explained in the next chapter.
此实例的详细情况将在下一章中作详细说明。


Correct Style Sheet Declaration
正确的样式表声明

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
我们使用根元素<xsl:stylesheet> 或<xsl:transform>对XSL样式表文档进行声明。

Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!
注意: <xsl:stylesheet> 和<xsl:transform>是完全相同的,使用哪一个都可以。

The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:
根据W3C XSLT 的推荐标准,声明 XSL 样式表的正确方法如下:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or:
或:

<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.
如果希望访问XSLT元素、属性和特征,那么,我们必须在文件的头部事先声明XSLT命名空间。

The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".
“xmlns:xsl="http://www.w3.org/1999/XSL/Transform” 指出了官方的W3C XSLT 命名空间。如果你使用了这个命名空间,你也必须注明属性版本“version="1.0"。


Start with a Raw XML Document
以XML源文件开始 

We want to transform the following XML document ("cdcatalog.xml") into XHTML:
让我们把下面的XML文档("cdcatalog.xml")转换成XHTML吧:

<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>

<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>

.
.
.
</catalog>

Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on a link) - The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.
在火狐和IE浏览器中查看XML文件的方法: 打开XML文件(通常情况下是通过链接打开的)—XML文件将会显示包含颜色代码的根元素和子元素。单击左边元素的“+”和“-”,就可以展开或者收回元 素结构列表。查看XML源文件的源代码(不含加号+和减号-),在浏览器菜单里选择“查看—源文件[ IE ]”或者“查看—页面源代码[ FF ]”。

Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements.
在Netscape 6中查看XML文件的方法:打开XML文件,在XML文件里右键单击选择“查看—页面源代码”。XML文件将会把标有颜色代码的根元素和子元素的文档显示出来。

Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select "Frame" / "View Source". The XML document will be displayed as plain text.
在Opera 7中查看XML文件的方法: 打开XML文件,在XML文件里右键单击选择“框架/查看源代码”。XML文件将会以纯文本的形式显示。

View "cdcatalog.xml"
查看 "cdcatalog.xml"


Create an XSL Style Sheet
创建一个XSL样式表

Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:
此时,你已经通过一个转换模板创建了一个XSL样式表文档("cdcatalog.xsl")。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>

<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>

<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>

</html>
</xsl:template>
</xsl:stylesheet>

View "cdcatalog.xsl"
查看"cdcatalog.xsl"


Link the XSL Style Sheet to the XML Document
将XSL样式表连接到XML文档中

Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):
把XSL样式表参数添加到XML文件("cdcatalog.xml")中:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd> . . .
</catalog>

If you have an XSLT compliant browser it will nicely transform your XML into XHTML.
如果你有一个支持XSLT的浏览器,它将会准确地帮你把XML文件转换成XHTML。

你可能感兴趣的:(XSL)