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样式表进行声明的根元素(root element)是<xsl:stylesheet> 或<xsl:transform>。

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的命名空间(namespace)。如果你使用了这个命名空间,你也必须注明属性版本(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源文件的源代码(没有加号和减号),在浏览器菜单里选择“查看页面源代码(View Page Source)”或者“查看源代码(View Source)”。

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文件里右键单击选择“查看页面源代码(View Page Source)”。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文件里右键单击选择“框架/查看源代码("Frame" / "View Source")”。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.
如果你有了一个合适的浏览器,它将会准确地帮你把XML文件转换成XHTML。

View the result
查看结果

The details of the example above will be explained in the next chapters.
以上实例的细节部分将会在下一章中作详细说明。

 

An XSL style sheet consists of one or more set of rules that are called templates.
XSL样式表是由一个或者更多的被称为“模板(templates)”的规则设置(set of rules)组成的。

Each template contains rules to apply when a specified node is matched.
当与指定的节点匹配时,每个模板都包含了应用规则。


The <xsl:template> Element
XSLT<xsl:template>元素

The <xsl:template> element is used to build templates.
XSLT<xsl:template>元素是用于创建模板的。

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
Match的属性的作用是使模板和XML与元素相结合。Match属性也可以为整个XML定义模版。Match属性值是一个XPath表达式。(也就是match="/" defines the whole document)

Ok, let's look at a simplified version of the XSL file from the previous chapter:
好了,现在让我们来看一下摘自前面章节的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>Title</th>
<th>Artist</th>

</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>

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

Since an XSL style sheet is an XML document itself, it always begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>.
因为XSL样式表是XML文档本身,所以它一般都从XML开始声明:<?xml version="1.0" encoding="ISO-8 59-1"?>

The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).
下一个元素<xsl:stylesheet>,把这份文档定义为XSLT样式表文档(跟版本号和XSLT命名空间属性一样)。

The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document.
<xsl:template>元素定义了一份模板。Match=”/”属性使模板和XML源文件的根目录联系起来。

The content inside the <xsl:template> element defines some HTML to write to the output.
<xsl:template>元素里面的内容定义一些HTML来对结果进行书写。

The last two lines define the end of the template and the end of the style sheet.
最后两行定义了模版的结束符和样式表的结束符。

The result of the transformation above will look like this:
通过上面的代码,转换的结果如下:

My CD Collection
我的CD集

TitleArtist
..

 

View the XML fileView the XSL file, and View the result
查看XML文件, 查看 XSL文件, 还有 看下最后的结果

The result from this example was a little disappointing, because no data was copied from the XML document to the output.
通过这个例子所产生的结果有点令人失望,因为没有将任何数据从XML文档中复制到结果当中。

In the next chapter you will learn how to use the <xsl:value-of> element to select values from the XML elements.
下一章当中,你将会学习如何通过<xsl:value-of>元素从XML元素中选择值。

The <xsl:value-of> element is used to extract the value of a selected node.
<xsl:value-of>元素用于选取选择节点值。


The <xsl:value-of> Element
<xsl:value-of>元素

The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:
<xsl:value-of>元素可以用来选取XML元素以及把它添加到已被转换的输出流里中去。

<?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>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>

<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body>
</html>

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

Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.
注意: 选择(select)属性值是一个XPath表达式。XPath表达式的作用类似于对文件系统进行操作,通过在其前段添加“/”来选择子目录。

The result of the transformation above will look like this:
通过上面运行的代码,转换结果如下:

My CD Collection
我的CD集

TitleArtist
Empire BurlesqueBob Dylan

View the XML file, View the XSL file, and View the result
XML 文件, XSL 文件, 以及 结果页面

The result from this example was also a little disappointing, because only one line of data was copied from the XML document to the output.
通过这个例子得到的结果同样让人有点失望,因为只有一行数据从XML文件中复制到结果。

In the next chapter you will learn how to use the <xsl:for-each> element to loop through the XML elements, and display all of the records.
在下一章中,你将会学到通过XML元素如何使用<xsl:for-each>元素来做循环以及显示所有的记录。

The <xsl:for-each> element allows you to do looping in XSLT.
<xsl:for-each>元素允许在XSLT里使用循环语句。


The <xsl:for-each> Element
<sxl:-each>元素

The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set:
XSL<xsl:for-each>元素的作用是:选择任何一个具有指定的节点设置(node-set)的XML元素。

<?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>Title</th>
<th>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>

Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.
注意: 选择(select)属性值是一个XPath的表达式值。XPath表达式主要用于类似文件系统的操作,而这个文件系统里是通过在前端添加“/”选择子目录。

The result of the transformation above will look like this:
上面转换结果如下:

My CD Collection
我的CD集

TitleArtist
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
Greatest HitsDolly Parton
Still got the bluesGary More
ErosEros Ramazzotti
One night onlyBee Gees
Sylvias MotherDr.Hook
Maggie MayRod Stewart
RomanzaAndrea Bocelli
When a man loves a womanPercy Sledge
Black angelSavage Rose
1999 Grammy NomineesMany
For the good timesKenny Rogers
Big Willie styleWill Smith
Tupelo HoneyVan Morrison
SoulsvilleJorn Hoel
The very best ofCat Stevens
StopSam Brown
Bridge of SpiesT`Pau
Private DancerTina Turner
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
The dock of the bayOtis Redding
Picture bookSimply Red
RedThe Communards
Unchain my heartJoe Cocker

View the XML file, View the XSL file, and View the result
XML 文件, XSL 文件, 以及 结果


Filtering the Output
过滤结果(Filtering the Output)

We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element.
我们也可以从XML文件通过将一个规则添加到<xsl:for-each>元素中的选择属性来过滤结果。

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

Legal filter operators are:
正规的过滤操作是:

  • =  (equal)
    =  (等于)
  • != (not equal)
    != (不等于)
  • &lt; less than
    < 小于
  • &gt; greater than
    > 大于

Take a look at the adjusted XSL style sheet:
让我们来看一下调整过的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>Title</th>
<th>Artist</th>

</tr>
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
<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>

The result of the transformation above will look like this:
上面的转换结果如下:

My CD Collection
我的CD集

TitleArtist
Empire BurlesqueBob Dylan

View the XML file, View the XSL file, View the result
XML文件, XSL文件, 结果

The <xsl:sort> element is used to sort the output.
<xsl:sort>元素用于对结果进行分类。


Where to put the Sort Information
在哪儿放置分类信息

To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file:
如果要对结果进行分类,可以在XSL文件里的<xsl:for-each>元素中添加一个<xsl:sort>元素。

<?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>Title</th>
<th>Artist</th>
</tr>

<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<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>

Note: The select attribute indicates what XML element to sort on.
注意: 选择(select)属性需要会告诉你哪些XML元素需要进行分类。

The result of the transformation above will look like this:
通过运行上面这段代码,我们可以看到的转换结果如下:

My CD Collection
我的CD集

TitleArtist
RomanzaAndrea Bocelli
One night onlyBee Gees
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
The very best ofCat Stevens
Greatest HitsDolly Parton
Sylvias MotherDr.Hook
ErosEros Ramazzotti
Still got the bluesGary Moore
Unchain my heartJoe Cocker
SoulsvilleJorn Hoel
For the good timesKenny Rogers
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
1999 Grammy NomineesMany
The dock of the bayOtis Redding
When a man loves a womanPercy Sledge
Maggie MayRod Stewart
StopSam Brown
Black angelSavage Rose
Picture bookSimply Red
Bridge of SpiesT`Pau
RedThe Communards
Private DancerTina Turner
Tupelo HoneyVan Morrison
Big Willie styleWill Smith

 

View the XML file, View the XSL file, and View the result
XML文件, XSL文件, 以及 结果

The <xsl:if> element is used to put a conditional test against the content of the XML file.
<xsl:if>元素的作用是:对XML文件的内容设置一个条件语句。


The <xsl:if> Element
<xsl:if>元素

To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.
如果你要对XML文件的内容设置一个条件语句,那需要向XSL文件中添加一个<xsl:if>元素。

Syntax
语法

<xsl:if test="expression">

...
...some output if the expression is true...
...
</xsl:if>


Where to Put the <xsl:if> Element
<xsl:if>元素应该放在哪

To add a conditional test, add the <xsl:if> element inside the <xsl:for-each> element in the XSL file:
要添加一个条件语句,那必须先在XSL文件里的<xsl:for-each>里添加一个<xsl:if>元素。

<?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>Title</th>
<th>Artist</th>

</tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price > 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Note: The value of the required test attribute contains the expression to be evaluated.
注意: 所要求的语句(test)属性值包括了要条件表达式。

The code above will only output the title and artist elements of the CDs that has a price that is higher than 10.
上面的代码仅仅会输出那些价格高于10的CD的标题和艺术家。

The result of the transformation above will look like this:
上面的转换结果如下:

My CD Collection
我的CD集

TitleArtist
Empire BurlesqueBob Dylan
Still got the bluesGary Moore
One night onlyBee Gees
RomanzaAndrea Bocelli
Black AngelSavage Rose
1999 Grammy NomineesMany

View the XML file, View the XSL file, and View the result
XML文件, XSL文件, 以及 结果

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.
<xsl:choose>元素通过<xsl:when>和<xsl:otherwise>一起配合是用来表达多种条件语句。


The <xsl:choose> Element
<xsl:choose>元素

Syntax
语法

<xsl:choose>

<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>


Where to put the Choose Condition
如何提出选择条件

To insert a multiple conditional test against the XML file, add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
对XML文件插入一个多重条件语句,将<xsl:choose>,<xsl:when>和<xsl:otherwise>元素添加到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>Title</th>
<th>Artist</th>
</tr>

<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price > 10">

<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>

</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

The code above will add a pink background-color to the "Artist" column WHEN the price of the CD is higher than 10.
当CD的价格高于10 时,上面的这些代码会将红色背景颜色添加到”Artist”专栏。

The result of the transformation will look like this:
转换结果如下:

My CD Collection
我的CD集

TitleArtist
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
Greatest HitsDolly Parton
Still got the bluesGary Moore
ErosEros Ramazzotti
One night onlyBee Gees
Sylvias MotherDr.Hook
Maggie MayRod Stewart
RomanzaAndrea Bocelli
When a man loves a womanPercy Sledge
Black angelSavage Rose
1999 Grammy NomineesMany
For the good timesKenny Rogers
Big Willie styleWill Smith
Tupelo HoneyVan Morrison
SoulsvilleJorn Hoel
The very best ofCat Stevens
StopSam Brown
Bridge of SpiesT`Pau
Private DancerTina Turner
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
The dock of the bayOtis Redding
Picture bookSimply Red
RedThe Communards
Unchain my heartJoe Cocker

View the XML file,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值