XSLT教程

XSL代表可扩充样式表语言
为了适应基于XML的样式表语言的需要,万维网联盟(W3C)开始发展XSL。
XSLT代表XSL的变革。在这个教程里,你会学到如何通过XSLT把XML文档转换成别的版式——例如XHTML。

XSLT 参考

XSLT 元素
来自W3C参考标准所定义(XSLT版本1.0)的XSLT元素。

XSLT 函数
XSLT包含了超过100个内置函数。有些是为了字符串值,数字值,数据和时间比较关系,节点和OName操作,序列操作,布尔(逻辑)值,以及更多。

内容目录

XSL 语言
Defines the sub-languages of XSL: XSLT, XPath and XSL-FO.
XSL的子语言:XSLT,XPath和XSL-FO。

XSLT 介绍
An introduction to XSLT. What it is, and what it can do.
有关XSLT的介绍,它的概念,以及它的用途

XSLT 浏览起器
Overview of browser support for XSLT.
了解下哪些浏览器支持XSLT

XSLT Transformation
How XSLT can be used to transform XML documents into XHTML documents.
怎样才能让XSLT将XML文档转换成为XHTML文档

XSLT Templates
The <xsl:template> element contains rules to apply when a specified node is matched.
当一个指定的点吻合的时候<xsl:template>元素所含规则就可以生效

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

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

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

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

XSLT <xsl:choose> 元素
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>一起配合是用来表达多种条件语句。

XSLT <xsl:apply-templates> 元素
The <xsl:apply-templates> element applies a template rule to the current element or to the current element's child nodes.
<xsl:apply-templates>元素是把模板应用到当前元素或当前元素的子节点上。

位于客户端的XSL
How to transform an XML document to an XHTML document on the client.
如果你的浏览器支持XSLT,它可以在你的浏览器中把文件转换成XHTML。

位于服务器端的XSL
How to transform an XML document to an XHTML document on the server.
因为不是所有的浏览器都支持XSLT,所以人们提出了一个在服务器端把XML转换成XHTML的解决方案。

XSL 语言
w3pop.com / 2006-09-19

It started with XSL and ended up with XSLT, XPath, and XSL-FO.
XSL含有三个子语言:XSLT,XPath和XSL-FO。


It Started with XSL
以XSL为开始

XSL stands for EXtensible Stylesheet Language.
XSL代表着可扩充样式表语言(EXtensible Stylesheet Language)。

The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.
因为基于XML样式表语言的需要,万维网联盟(W3C)开始发展XSL。


CSS = HTML Style Sheets
层叠式样式表(CSS)= HTML样式表

HTML uses predefined tags and the meaning of the tags are well understood.
HTML使用预定的标签,标签的含义比较容易理解。

The <table> element in HTML defines a table - and a browser knows how to display it.
在HTML中,<table>元素定义了一个表格,浏览器知道如何去显示它。

Adding styles to HTML elements is simple. Telling a browser to display an element in a special font or color, is easy with CSS.
给HTML元素定义样式是比较容易的。通过使用CSS,告诉浏览器对特殊字体和颜色的元素进行显示,是非常容易的。


XSL = XML Style Sheets
XSL=XML样式表

XML does not use predefined tags (we can use any tag-names we like), and the meaning of these tags are not well understood.
XML不使用预定的标签(我们可以使用任何我们自己喜欢的标签名字),这些标签的含义比较难以理解。

The <table> element in HTML defines a table - and a browser knows how to display it.
<table>元素可以表示HTML表格,一种设备,或者别的一些东西,因此浏览器不知道如何去显示它们。

Adding styles to HTML elements is simple. Telling a browser to display an element in a special font or color, is easy with CSS.
XSL描述的是如何显示XML文档。


XSL - More Than a Style Sheet Language
XSL-超越样式表语言(More Than a Style Sheet Language)

XSL consists of three parts:
XSL由3部分组成:

  • XSLT - a language for transforming XML documents
    XSLT-转变XML文档的语言。
  • XPath - a language for navigating in XML documents
    XPath-对XML文档进行操作的语言。
  • XSL-FO - a language for formatting XML documents
    XSL-FO - 格式化XML文档的语言。

This Tutorial is About XSLT

针对XSLT的教程

The rest of this tutorial is about XSLT - the language for transforming XML documents.
剩下这部分的教程是关于XSLT的——转换XML文件的语言。

But you can also study our XPath Tutorial and our XSL-FO Tutorial.
同样你还可以学习 XPath 教程 和我们的 XSL-FO 教程.

XSLT 介绍
w3pop.com / 2006-09-20

XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.
XSLT是一种把XML文件转换成XHTML文档或者其他的XML文档的语言。

XPath is a language for navigating in XML documents.
XPath是一种由于操作XML文档的语言。


What You Should Already Know
学前基础

Before you continue you should have a basic understanding of the following:
如果你想继续下面的学习,你必须得掌握下面的一些基本常识。

  • HTML / XHTML
  • XML / XML 命名空间
  • XPath

If you want to study these subjects first, find the tutorials on our Home page.
如果你想要先学习这些课题,那请在知识库找到这些教程.


What is XSLT?
什么是XSLT?

  • XSLT stands for XSL Transformations
    XSLT代表XSL转换(XSL Transformations)
  • XSLT is the most important part of XSL
    XSLT是XSL最重要的部分
  • XSLT transforms an XML document into another XML document
    XSLT可以把XML文档转换成另一个XML文档
  • XSLT uses XPath to navigate in XML documents
    XSLT通过XPath操作XML文档
  • XSLT is a W3C Recommendation
    XSLT是一种W3C参考标准

XSLT = XSL Transformations
XSLT=XSL转换(XSL Transformations)

XSLT is the most important part of XSL.
XSLT是XSL的最重要的一部分。

XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.
XSLT用于把XML文件转换成另一份XML文件,或者转换成另一种被浏览器所识别的诸如HTML和XHTML类型的文件。通常情况下,XSLT是通过把每个XML元素转换成(X)HTML文件来完成的。

With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.
通过XSML,你可以从已输出的文件里添加/移除元素和属性。你也可以把元素重新排列和分类,执行测试语句,决定是隐藏还是显示元素,或者实现其它更多的功能。

A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.
对于描述转换过程,可以形象的描述为:XSLT把XML源树转(XML source-tree)换成XML结果树(XML result-tree)。


XSLT Uses XPath
XSLT对于XPath的使用

XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.
XSLT通过对XPath的使用来找寻XML文档中的信息。XPath用于操作XML文件中的元素和属性。

If you want to study XPath first, please read our XPath Tutorial.
如果你想先学习XPath,请先阅读我们的XPath教程.


How Does it Work?
如何使它运行?

In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.
在转换过程中,XSLT通过XPath来定义部分应该和一个或更多预定的模板相匹配的源文件。当一个匹配找到后,XSLT将会把相匹配的部分源文件转换成结果文档。


XSLT is a Web Standard
XSLT是一个网络标准

XSLT became a W3C Recommendation 16. November 1999.
XSLT在1999年11月16日成为W3C参考标准。

To read more about the XSLT activities at W3C, please read our W3C Tutorial.
如果想了解更多关于W3C上的XSLT活动,请阅读我们的 W3C教程。

XSLT 浏览器
w3pop.com / 2006-09-20

Nearly all major browsers have support for XML and XSLT.
几乎所有专业浏览器都支持XML和XSLT。


Mozilla Firefox
Mozilla火狐

As of version 1.0.2, Firefox has support for XML and XSLT (and CSS).
就如1,0,2版本的火狐是支持XML和XSLTi(以及CSS)。


Mozilla

Mozilla includes Expat for XML parsing and has support to display XML + CSS. Mozilla also has some support for Namespaces.
Mozilla含有XML外部解析器并且支持XML+CSS的显示,Mozilla同时也支持一些命名空间(namespace)。

Mozilla is available with an XSLT implementation.
Mozilla可执行XSLT。


Netscape

As of version 8, Netscape uses the Mozilla engine, and therefore it has the same XML / XSLT support as Mozilla.
Netscape版本8使用Mozilla引擎,因此它和Mozilla一样支持XML/XSLT。


Opera

As of version 9, Opera has support for XML and XSLT (and CSS). Version 8 supports only XML + CSS.
Opera版本9 ,支持XML和XSLT(以及CSS);Opera版本8只支持XML+CSS。


Internet Explorer

As of version 6, Internet Explorer supports XML, Namespaces, CSS, XSLT, and XPath.
IE6浏览器支持XML,Namespaces,CSS,XSLT和XPath。

Version 5 is NOT compatible with the official W3C XSL Recommendation.
IE5.0和The official W3C XSL参考标准并不是一样的。

XSLT 转换
w3pop.com / 2006-09-20

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.
以上实例的细节部分将会在下一章中作详细说明。

<xsl:template>
w3pop.com / 2006-09-20

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集

Title Artist
. .

View the XML file, View 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元素中选择值。

<xsl:value-of>
w3pop.com / 2006-09-20

<xsl:template> <xsl:for-each>

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集

Title Artist
Empire Burlesque Bob 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>元素来做循环以及显示所有的记录。

<xsl:for-each>
w3pop.com / 2006-09-20

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集

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
Greatest Hits Dolly Parton
Still got the blues Gary More
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe 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集

Title Artist
Empire Burlesque Bob Dylan

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

<xsl:sort>
w3pop.com / 2006-09-20

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集

Title Artist
Romanza Andrea Bocelli
One night only Bee Gees
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
The very best of Cat Stevens
Greatest Hits Dolly Parton
Sylvias Mother Dr.Hook
Eros Eros Ramazzotti
Still got the blues Gary Moore
Unchain my heart Joe Cocker
Soulsville Jorn Hoel
For the good times Kenny Rogers
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
1999 Grammy Nominees Many
The dock of the bay Otis Redding
When a man loves a woman Percy Sledge
Maggie May Rod Stewart
Stop Sam Brown
Black angel Savage Rose
Picture book Simply Red
Bridge of Spies T`Pau
Red The Communards
Private Dancer Tina Turner
Tupelo Honey Van Morrison
Big Willie style Will Smith

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

<xsl:if>
w3pop.com / 2006-09-20

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集

Title Artist
Empire Burlesque Bob Dylan
Still got the blues Gary Moore
One night only Bee Gees
Romanza Andrea Bocelli
Black Angel Savage Rose
1999 Grammy Nominees Many

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

<xsl:choose>
w3pop.com / 2006-09-20

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集

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
Greatest Hits Dolly Parton
Still got the blues Gary Moore
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe Cocker

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

Another Example
另外一个举例

Here is another example that contains two <xsl:when> elements:
这里是另外一个包含2个<xsl:when>元素的例子。

<?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:when test="price < 9">        <td bgcolor="#cccccc">        <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, and a grey background-color WHEN the price of the CD is higher than 9 and lower or equal to 10.
当CD的价格高于10时,上面的这些代码会将红色背景颜色添加到“Artist”专栏;当CD的价格大于9小于等于10时,会将灰色背景颜色添加到“Artist”专栏。

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

My CD Collection
我的CD集

Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
Greatest Hits Dolly Parton
Still got the blues Gary Moore
Eros Eros Ramazzotti
One night only Bee Gees
Sylvias Mother Dr.Hook
Maggie May Rod Stewart
Romanza Andrea Bocelli
When a man loves a woman Percy Sledge
Black angel Savage Rose
1999 Grammy Nominees Many
For the good times Kenny Rogers
Big Willie style Will Smith
Tupelo Honey Van Morrison
Soulsville Jorn Hoel
The very best of Cat Stevens
Stop Sam Brown
Bridge of Spies T`Pau
Private Dancer Tina Turner
Midt om natten Kim Larsen
Pavarotti Gala Concert Luciano Pavarotti
The dock of the bay Otis Redding
Picture book Simply Red
Red The Communards
Unchain my heart Joe Cocker

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

<xsl:apply-templates>
w3pop.com / 2006-09-20

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
<xsl:apply-templates>元素是把模板应用到当前元素或当前元素的子节点上。


The <xsl:apply-templates> Element
<xsl:apply-templares>元素

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
<xsl:apply-templates>元素是把模板应用到当前元素或当前元素的子节点上。

If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.
如果我们向只处理与属性值相匹配的子元素的<xsl:apply-templates>中添加一个选择属性,我们可以使用选择属性来详细说明处理子节点的顺序。

Look at the following 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> <xsl:apply-templates/> </body></html></xsl:template>
<xsl:template match="cd">

<p><xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/></p></xsl:template>
<xsl:template match="title">

Title: <span style="color:#ff0000"><xsl:value-of select="."/></span><br /></xsl:template>
<xsl:template match="artist">

Artist: <span style="color:#00ff00"><xsl:value-of select="."/></span><br /></xsl:template>
</xsl:stylesheet>

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

My CD Collection
我的CD集

Title: Empire Burlesque
Artist: Bob Dylan

Title: Hide your heart
Artist: Bonnie Tyler

Title: Greatest Hits
Artist: Dolly Parton

Title: Still got the blues
Artist: Gary Moore

Title: Eros
Artist: Eros Ramazzotti

Title: One night only
Artist: Bee Gees

Title: Sylvias Mother
Artist: Dr.Hook

Title: Maggie May
Artist: Rod Stewart

Title: Romanza
Artist: Andrea Bocelli

Title: When a man loves a woman
Artist: Percy Sledge

Title: Black angel
Artist: Savage Rose

Title: 1999 Grammy Nominees
Artist: Many

Title: For the good times
Artist: Kenny Rogers

Title: Big Willie style
Artist: Will Smith

Title: Tupelo Honey
Artist: Van Morrison

Title: Soulsville
Artist: Jorn Hoel

Title: The very best of
Artist: Cat Stevens

Title: Stop
Artist: Sam Brown

Title: Bridge of Spies
Artist: T`Pau

Title: Private Dancer
Artist: Tina Turner

Title: Midt om natten
Artist: Kim Larsen

Title: Pavarotti Gala Concert
Artist: Luciano Pavarotti

Title: The dock of the bay
Artist: Otis Redding

Title: Picture book
Artist: Simply Red

Title: Red
Artist: The Communards

Title: Unchain my heart
Artist: Joe Cocker

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

XSLT - 位于客户端
w3pop.com / 2006-09-20

If your browser supports it, XSLT can be used to transform the document to XHTML in your browser.
如果你的浏览器支持XSLT,它可以在你的浏览器中把文件转换成XHTML。


A JavaScript Solution
JavaScript解决方案

In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.
在先前一章,我们已经解释了怎么用XSLT把一份文档从XML转换成XHTML。我们通过向XML文件中添加一个XSL样式表,并让浏览器对其进行转换。

Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.)
即使这样做运行得不错,你也不可以绝对地将其视为一个XML文件的样式参考(比如,在不支持XSLT的浏览器中,它将不会运行)。

A more versatile solution would be to use a JavaScript to do the transformation.
更通用的解决方案是使用JavaScript来做转换。

By using a JavaScript, we can:
通过使用JavaScript,我们可以:

  • do browser-specific testing
    做浏览器的细节(browser-specific)测试
  • use different style sheets according to browser and user needs
    根据浏览器和使用者的需要使用不同的样式表。

That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.
那是XSLT的魅力。其中一个XSLT的设计目标就是为了在支持不同浏览器和不同使用者需求的情况下,使它实现从一个格式到另一个格式的数据转换。

XSLT transformation on the client side is bound to be a major part of the browsers work tasks in the future, as we will see a growth in the specialized browser market (Braille, aural browsers, Web printers, handheld devices, etc.)
在客户端进行XSLT的转换一定是未来浏览器工作任务的主要部分,我们将会在专业的浏览器市场看到它的成长(与盲人对应的听觉浏览器, 网络打印机, 手动驱动等等)。


The XML File and the XSL File
XML文件和XSL文件

Look at the XML document that you have seen in the previous chapters:
来看一下已经在前一章看过的XML文件。

<?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>

View the XML file.
XML文件.

And the accompanying 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 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 the XSL file.
查看XSL文件.

Notice that the XML file does not have a reference to the XSL file.
注意XML文件与XSL文件并无关联。

IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL style sheets.
重点: 上面的句子指出,一个XML文件可以通过使用许多不同的XSL样式表进行转换。


Transforming XML to XHTML in the Browser
在浏览器中把XML文件转换成XHTML

Here is the source code needed to transform the XML file to XHTML on the client:
这儿的源代码需要在客户端把XML文件转换成XHTML。

<html><body>
<script type="text/javascript">
// Load XML var xml = new ActiveXObject("Microsoft.XMLDOM")xml.async = falsexml.load("cdcatalog.xml")
// Load XSLvar xsl = new ActiveXObject("Microsoft.XMLDOM")xsl.async = falsexsl.load("cdcatalog.xsl")
// Transformdocument.write(xml.transformNode(xsl))
</script>
</body></html>

Tip: If you don't know how to write JavaScript, you can study our JavaScript tutorial.
提示:
如果你不知道怎么写JavaScript,你可以看一下我们的JavaScript 教程.

The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory. The second block of code creates another instance of the parser and loads the XSL file into memory. The last line of code transforms the XML document using the XSL document, and displays the result as XHTML in your browser. Nice!
第一块代码创建了一个微软的XML文件解析器(XMLDOM)的实例,并把XML文件加载到存储器中。第二块代码创建了另外一个解析器的实例,并且把XML文件加载到存储器中。最后一行的代码通过使用XSL文件来转换XML文件,并在浏览器中以XHTML的形式显示结果。

See how it works in IE.
了解其在IE中的运行情况。

服务器上的XSLT
w3pop.com / 2006-09-20

Since not all browsers support XSLT, one solution is to transform the XML to XHTML on the server.
因为不是所有的浏览器都支持XSLT,所以人们提出了一个在服务器端把XML转换成XHTML的解决方案。


A Cross Browser Solution
一个交叉浏览器解决方案

In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We created a JavaScript that used an XML parser to do the transformation. The JavaScript solution will not work in a browser that doesn't have an XML parser.
在前一章我们解释了怎样在浏览器中用XSLT把文件从XML转换成XHTML。我们创建了一个用XML解析器来做转换的JavaScript。JavaScript解决程序将不会在没有XML解析器的浏览器上运行。

To make XML data available to all kind of browsers, we must transform the XML document on the SERVER and send it as XHTML back to the browser.
想使XML数据对各种浏览器都适用,我们必须在服务器上转换XML文件,并把它以XHTML的形式发送回浏览器。

That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.
这是XSLT的另一个魅力。XSLT的其中一个设计目标就是能够让它在服务器端将数据从一种格式转化为另一种格式,并把所有可读的数据返回到各种浏览器上。


The XML File and the XSLT File
XML文件和XSLT文件

Look at the XML document that you have seen in the previous chapters:
下面我们来回顾一下前一章使用过的XML文件:

<?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>

View the XML file.
查看XML文件。.

And the accompanying 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 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 the XSL file.
XSL文件.

Notice that the XML file does not have a reference to the XSL file.
注意XML文件和XSL文件并无关联

IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL style sheets.
重点:上面的句子指出:XML文件不应该通过不同的XSL样式表进行转换。


Transforming XML to XHTML on the Server
在服务器端把XML转换成XHTML

Here is the ASP source code needed to transform the XML file to XHTML on the server:
这儿的动态服务器主页(ASP)源代码需要在服务器端把XML文件转换成XHTML。

<%'Load XMLset xml = Server.CreateObject("Microsoft.XMLDOM")xml.async = falsexml.load(Server.MapPath("cdcatalog.xml"))

'Load XSLset xsl = Server.CreateObject("Microsoft.XMLDOM")xsl.async = falsexsl.load(Server.MapPath("cdcatalog.xsl"))

'Transform fileResponse.Write(xml.transformNode(xsl))%>

Tip: If you don't know how to write ASP, you can study our ASP tutorial.
提示:
如果你不知道怎样书写ASP,你可以了解一下ASP 教程

The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory. The second block of code creates another instance of the parser and loads the XSL file into memory. The last line of code transforms the XML document using the XSL document, and sends the result as XHTML to your browser. Nice!
第一块代码创建了一个微软XML解析器(XMLDOM)的实例,并把此文件加载到存储器里。第二块代码创建了另外一个解析器的实例,并把XSL文件加载到存储器中。最后一行代码通过使用XSL文件来转换XML文件,并将其以XHTML的形式把结果发到浏览器上。真棒!

See how it works.
看它是怎样工作的.

XSLT - 编辑XML
w3pop.com / 2006-09-20

服务器上的XSLT XSLT 摘要

Data stored in XML files can be edited from an Internet browser.
储存在XML文件中的数据可以在英特网浏览器上进行编辑。


Open, Edit and Save XML
打开、编辑和保存XML的方法

Now, we will show how to open, edit, and save an XML file that is stored on the server.
现在我们将演示一下如何打开、编辑和保存一份储存在服务器上的XML文件。

We will use XSL to transform the XML document into an HTML form. The values of the XML elements will be written to HTML input fields in an HTML form. The HTML form is editable. After editing the data, the data is going to be submitted back to the server and the XML file will be updated (this part is done with ASP).
我们将用XSL来把XML 文档转换成HTML形式。XML元素值将以HTML的格式写入HTML输入域(input field)。HTML的格式是可以被编辑的。数据被编辑之后将被重新传回服务器,XML也将被升级(这份HTML元素值将被写到HTML表格中的HTML输入栏)。


The XML File and the XSL File
XML文件和XSL文件

First, look at the XML document that will be used ("tool.xml"):
首先,来看一下要用到的 XML文件("tool.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?><tool><field id="prodName"><value>HAMMER HG2606</value> </field><field id="prodNo"><value>32456240</value> </field><field id="price"><value>$30.00</value> </field></tool>

View the XML file.
XML文件.

Then, take a look at the following style sheet ("tool.xsl"):
让我们看一下下面的样式表("tool.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>
<form method="post" action="edittool.asp">
<h2>Tool Information (edit):</h2>
<table border="0"><xsl:for-each select="tool/field">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<input type="text"><xsl:attribute name="id"><xsl:value-of select="@id" />
</xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="@id" /></xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="value" />
</xsl:attribute>
</input>
</td></tr>
</xsl:for-each>
</table>
<br />
<input type="submit" id="btn_sub" name="btn_sub" value="Submit" /><input type="reset" id="btn_res" name="btn_res" value="Reset" />
</form>
</body></html>
</xsl:template>
</xsl:stylesheet>

View the XSL file.
XSL文件.

The XSL file above loops through the elements in the XML file and creates one input field for each XML "field" element. The value of the XML "field" element's "id" attribute is added to both the "id" and "name" attributes of each HTML input field. The value of each XML "value" element is added to the "value" attribute of each HTML input field. The result is an editable HTML form that contains the values from the XML file.
上面的XSL文件对XML文件里的元素进行循环,并为每一个XML域(field)创建了输入域(input field)。XML“域(field)”元素的ID属性值被添加到每个HTML输入域(input field)的“ID”和“名字”属性中。每个XML“值(value)”元素的值要被添加到每个HTML输入域的“值”属性里。结果是一个可编辑的HTML表格包含了来自XML文件中的值。

Then, we have a second style sheet: "tool_updated.xsl". This is the XSL file that will be used to display the updated XML data. This style sheet will not result in an editable HTML form, but a static HTML table:
接下来,我们开始第二个样式表:"tool_updated.xsl"。这是用来显示最新的XML数据的XSL文件。这个样式表不会产生一个可编辑HTML表格,但是会产生一个静态的HTML表格:

<?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>Updated Tool Information:</h2><table border="1">
<xsl:for-each select="tool/field">
<tr>
<td><xsl:value-of select="@id" /></td>
<td><xsl:value-of select="value" /></td></tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

View the XSL file.
XSL文件.


The ASP File
ASP文件

The HTML form in the "tool.xsl" file above has an action attribute with a value of "edittool.asp".
上面"tool.xsl" 文件中,HTML的构成具有"edittool.asp"值的行为属性。

The "edittool.asp" page contains two functions: The loadFile() function loads and transforms the XML file for display and the updateFile() function applies the changes to the XML file:
"edittool.asp"页包含了2个函数:loadFile()函数是用来加载和转换XML文件的,目的在于将其显示出来;updateFile()函数的作用是把所有的这些改变应用于XML文件。

<%
function loadFile(xmlfile,xslfile)
Dim xmlDoc,xslDoc
'Load XML file
set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load(xmlfile)
'Load XSL file
set xslDoc = Server.CreateObject("Microsoft.XMLDOM")
xslDoc.async = false
xslDoc.load(xslfile)
'Transform file
Response.Write(xmlDoc.transformNode(xslDoc))
end function
function updateFile(xmlfile)
Dim xmlDoc,rootEl,f
Dim i
'Load XML file
set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = false
xmlDoc.load(xmlfile)
'Set the rootEl variable equal to the root element
Set rootEl = xmlDoc.documentElement
'Loop through the form collection
for i = 1 To Request.Form.Count'Eliminate button elements in the formif instr(1,Request.Form.Key(i),"btn_")=0 then'The selectSingleNode method queries the XML file for a 'single node that matches a query. This query requests'the value element that is the child of a field element'that has an id attribute which matches the current key'value in the Form Collection. When there is a match -'set the text property equal to the value of the current'field in the Form Collection.set f = rootEl.selectSingleNode("field[@id='" & _Request.Form.Key(i) & "']/value")f.Text = Request.Form(i)end if
next
'Save the modified XML file
xmlDoc.save xmlfile
'Release all object references
set xmlDoc=nothing
set rootEl=nothing
set f=nothing
'Load the modified XML file with a style sheet that
'allows the client to see the edited information
loadFile xmlfile,server.MapPath("tool_updated.xsl")
end function
'If the form has been submitted update the
'XML file and display result - if not,
'transform the XML file for editing
if Request.Form("btn_sub")="" then
loadFile server.MapPath("tool.xml"),server.MapPath("tool.xsl")
else
updateFile server.MapPath("tool.xml")
end if
%>

Tip: If you don't know how to write ASP, you can study our ASP tutorial.
提示: 如果你还不会写ASP文件的话请学习我们的ASP 教程.

Note: We are doing the transformation and applying the changes to the XML file on the server. This is a cross-browser solution. The client will only get HTML back from the server - which will work in any browser.
注意: 我们可以进行转换,并把这些改变应用到在服务器上的XML文件。这将是一个交互式浏览器的解决方案。客户端只要从浏览器获取HTML值就可以了,而且所有的浏览器都具备这样的功能。

XSLT 摘要
w3pop.com / 2006-09-20

This tutorial has taught you how to use XSLT to transform XML documents into other formats, like XHTML.
这份教程已经教会了你如何通过XSLT把XML文档转换成其他格式。诸如:XHTML。

You have learned how to add/remove elements and attributes to or from the output file.
你已经学会了怎么从结果文件里添加/移除元素和属性或者将元素和属性添加/移除到结果文件里。

You have also learned how to rearrange and sort elements, perform tests and make decisions about which elements to hide and display.
你也已经学会了怎么再排列和分类元素,完成测试以及决定隐藏或显示哪些元素。

For more information on XSLT, please look at our XSLT reference.
如想获取更多XSLT信息,请阅读XSLT 参考.


Now You Know XSLT, What's Next?
现在你知道了XSLT,那下一步呢?

XSL includes 3 languages: XSLT, XPath and XSL-FO, so the next step is to learn about XPath and XSL-FO.
XSL包含了3种语言:XSLT, XPath 和XSL-FO,所以下一步应该学习的是XPath 和XSL-FO。

XPath

XPath is used to navigate through elements and attributes in an XML document.
XPath作用是XML文件中的元素和属性进行操作。

XPath is a major element in the W3C's XSL standard. An understanding of XPath is fundamental for advanced use of XML.
XPath是W3C XSL标准中的一个主要元素。掌握XPath进一步开发XML的基础。

Without any XPath knowledge, you will not be able to create XSLT documents.
没有任何XPath这方面的知识,你不可能创建XSLT文件。

If you want to learn more about the XPath, please visit our XPath tutorial.
如果你想学习更多XPath,请访问我们的 XPath 教程.

XSL-FO

XSL-FO describes the formatting of XML data for output to screen, paper or other media.
XSL-FO描述的是输出到屏幕、文件、以及其他媒体的XML数据的格式。

XSL-FO documents are XML files with information about the output layout and output content.
XSL-FO文件是含有关于输出版面和输出内容信息的XML文件。

If you want to learn more about XSL-FO, please visit our XSL-FO tutorial.
如果你想学到更多XSL-FO,请访问我们的 XSL-FO 教程.

XSLT 元素参考
w3pop.com / 2006-09-20

The XSLT elements from the W3C Recommendation (XSLT Version 1.0).
来自W3C参考标准所定义(XSLT版本1.0)的XSLT元素。


XSLT Elements
XSLT元素

The links in the "Element" column point to attributes and more useful information about each specific element.
“元素”列中的链接指出每个特殊元素的属性和更多有用的信息。

  • N: indicates the earliest version of Netscape that supports the tag
    N: 指出Netscape最早版本的支持标签。
  • IE: indicates the earliest version of Internet Explorer that supports the tag
    IE: 指出最早的支持标签的IE版本。

Note: Elements supported in IE 5 may have NON-standard behavior, because IE 5 was released before XSLT became an official W3C Recommendation.
注意: 在IE5中被支持的元素可能含有非标准的行为(NON-standard behavior),因为IE5在XSLT成为W3C的官方参考标准之前就已经发布了。

Element
元素
Description
描述
IE N
apply-imports Applies a template rule from an imported style sheet
应用输入样式表的模版规则
6.0  
apply-templates Applies a template rule to the current element or to the current element's child nodes
把模版规则应用到当前元素或当前元素的子元素
5.0 6.0
attribute Adds an attribute
添加属性
5.0 6.0
attribute-set Defines a named set of attributes
定义指定属性
6.0 6.0
call-template Calls a named template
调用指定模版
6.0 6.0
choose Used in conjunction with <when> and <otherwise> to express multiple conditional tests
通过与<when> 和<otherwise>配合使用来表达多种条件测试
5.0 6.0
comment Creates a comment node in the result tree
在结果树中创建一个注释节点
5.0 6.0
copy Creates a copy of the current node (without child nodes and attributes)
复制正确节点(不需要子节点和属性)
5.0 6.0
copy-of Creates a copy of the current node (with child nodes and attributes)
复制正确节点(需要子节点和属性)
6.0 6.0
decimal-format Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function
定义字符和符号(在通过format-number() 函数,把数字转换成字符的情况下)
6.0  
element Creates an element node in the output document
在输出文档中创建一个元素节点
5.0 6.0
fallback Specifies an alternate code to run if  the processor does not support an XSLT element
如果处理器不支持XSLT元素,则运行指定的预备代码
6.0  
for-each Loops through each node in a specified node set
在指定的节点设置中通过每个节点做循环
5.0 6.0
if Contains a template that will be applied only if a specified condition is true
包含一个只当指定条件为true时才应用的模版
5.0 6.0
import Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet
把一个样式表内容输入到另外一个样式表去。注意:与已输入的样式表相比,正在输入的样式表更具有优先权。
6.0 6.0
include Includes the contents of one style sheet into another. Note: An included style sheet has the same precedence as the including style sheet
把一份样式表的内容嵌到另一份中去 。注意:这两份样式表有相同的优先级。
6.0 6.0
key Declares a named key that can be used in the style sheet with the key() function
通过key() 函数,声明可以用于样式表的指定键
6.0 6.0
message Writes a message to the output (used to report errors)
编写信息到输出(用于报告错误)
6.0 6.0
namespace-alias Replaces a namespace in the style sheet to a different namespace in the output
在样式表中把命名空间(namespace)替换成一个不同的命名空间输出
6.0  
number Determines the integer position of the current node and formats a number
确定当前节点和格式数字的整形位置
6.0 6.0
otherwise Specifies a default action for the <choose> element
指定<choose>元素的错误行为
5.0 6.0
output Defines the format of the output document
定义输出文件的格式
6.0 6.0
param Declares a local or global parameter
声明本地或是全球的参数
6.0 6.0
preserve-space Defines the elements for which white space should be preserved
定义储存空白页的元素
6.0 6.0
processing-instruction Writes a processing instruction to the output
编写输出的处理结构
5.0 6.0
sort Sorts the output
分类输出
6.0 6.0
strip-space Defines the elements for which white space should be removed
定义移除空白页的元素
6.0 6.0
stylesheet Defines the root element of a style sheet
定义样式表的根元素
5.0 6.0
template Rules to apply when a specified node is matched
当指定节点匹配时应用的规则
5.0 6.0
text Writes literal text to the output
编写文字文本到输出
5.0 6.0
transform Defines the root element of a style sheet
定义样式表的根元素
6.0 6.0
value-of Extracts the value of a selected node
摘取选定节点值
5.0 6.0
variable Declares a local or global variable
定义本地或全球变量
6.0 6.0
when Specifies an action for the <choose> element
指定<choose>元素的行为
5.0 6.0
with-param Defines the value of a parameter to be passed into a template
定义模版内的参数值
6.0 6.0

XSLT 函数
w3pop.com / 2006-09-20

XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same functions library.
XQuery 1.0, XPath 2.0, 和XSLT 2.0共享一个函数库。


XSLT Functions
XSLT函数

XSLT includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.
XSLT包含了超过100个内置函数。有些是为了字符串值,数字值,数据和时间比较关系,节点和OName操作,序列操作,布尔(逻辑)值,以及更多。

The URI of the XSLT function namespace is:http://www.w3.org/2005/02/xpath-functions
XSLT函数命名空间的URI是http://www.w3.org/2005/02/xpath-functions

The default prefix for the function namespace is fn:.
函数命名空间的默认前缀是“fn”:

Tip: Functions are often called with the fn: prefix, such as fn:string(). However, since fn: is the default prefix of the namespace, the function names do not need to be prefixed when called.
提示: 函数经常用“fn”命名:前缀,就像“fn:string()”;当然,自“fn:”后,是命名空间(namespace)的默认前缀,但当被访问时,函数名称不需要前缀。

The reference of all the built-in XSLT 2.0 functions is located in our XPath tutorial.
在XSLT2.0 函数中所有内置的参数都在我们XPath教程里面。

In addition, there are the following built-in XSLT functions:
另外,还有下面的内置XSLT函数:

Name
名称
Description
描述
current() Returns the current node
返回当前节点
document() Used to access the nodes in an external XML document
用来访问一个外部的XML文档内的节点
element-available() Tests whether the element specified is supported by the XSLT processor
测试指定的元素是否被XSLT处理器支持
format-number() Converts a number into a string
将数字转化为字符串
function-available() Tests whether the function specified is supported by the XSLT processor
测试指定的函数是否被XSLT处理器支持
generate-id() Returns a string value that uniquely identifies a specified node
返回一个独立地定义一个指定节点的字符串值
key() Returns a node-set using the index specified by an <xsl:key> element
使用由<xsl:key>元素指定的索引来返回一个节点设置(node-set)
system-property() Returns the value of the system properties
返回系统属性值
unparsed-entity-uri() Returns the URI of an unparsed entity
返回一个未被解析的URI实体

转载于:https://www.cnblogs.com/xujiaci/archive/2007/08/31/877198.html

XSLT教程 [转]相关推荐

  1. XML基础教程(2)

    一.XML元素 XML 文档包含 XML 元素. 什么是 XML 元素? XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分. 元素可包含其他元素.文本或者两者的混合物.元素也可以拥有 ...

  2. W3School 教程整理

    离线版大部分整理自 w3cschool ,少部分整理自w3school ,由本人整理. 感谢 w3cschool 站长的无私奉献. 由于本人精力有限,此处的列表不一定能够及时更新,最新版本请到本人博客 ...

  3. W3School教程整理

    W3School 教程整理 前端教程合集 在线浏览 AngularJS教程 Bootstrap教程 CSS参考手册 Foundation 教程 Highcharts 教程 HTML教程 CSS教程 F ...

  4. 您在w3schools上没有学过的XSLT技巧

    以下是一些有用的xslt通用技巧的简短列表,而这些技巧在w3schools上并未讲授. 属性值模板 W3C官方说明和示例 这是当您要将动态值放在元素的属性中时. 除了使用<xsl:attribu ...

  5. xslt_您在w3schools上没有学过的XSLT技巧

    xslt 这是一些有用的xslt通用技巧的简短列表,而这些技巧在w3schools上并未讲授. 属性值模板 W3C官方说明和示例 这是当您要将动态值放在元素的属性中时. 除了使用<xsl:att ...

  6. 使用 XSLT 显示 XML

    通过使用 XSLT,您可以向 XML 文档添加显示信息. 使用 XSLT 显示 XML XSLT 是首选的 XML 样式表语言. XSLT (eXtensible Stylesheet Languag ...

  7. xml命名空间只是标记区分不同的空间的作用,一般与上网无关,虽然通常都写的是URL网址

    XML 命名空间 XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 XML 携带 HTML ...

  8. 雷林鹏分享:XML 命名空间

    XML 命名空间 XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 XML 携带 HTML ...

  9. One Day-XML:XPath

    XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointe ...

最新文章

  1. C++用数组和链表分别实现Queue
  2. fiddler工具之Filters
  3. 【SDL的编程】VC环境搭建
  4. 模拟航班查询及预定系统 编写示例
  5. python 人气高的项目_给大家推荐:五个Python小项目,Github上的人气很高的!
  6. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
  7. JAVA获得天气json数据的方法,获取从天气预报接口返回回来的json数据
  8. 最新!2001-2021武书连中国大学排行榜Top 20
  9. 关于Request.ServerVariables(HTTP_REFERER)
  10. Windows10安装ubuntu 20双系统
  11. JAVA编程规范-命名规范
  12. 《Android游戏开发详解》一3.7 里程碑
  13. C++笔记------数据类型
  14. mysql 双机备份_Mysql双机热备份
  15. 手机计算机怎样调音乐,手机音乐EQ均衡器调节音效简单小技巧-均衡器设置
  16. pythonmathcot函数_sin cos tan cot公式
  17. java中的递归算法_java递归算法实例分析
  18. python测试自动化封装_python接口自动化学习笔记(封装获取测试数据方法)
  19. python常用内置函数乘法_每个 Python 高手都应该知道的内置函数
  20. 将文字生成png图片

热门文章

  1. 天地图专题三:根据标注点的范围确定天地图的中心点和缩放级别
  2. 通过分析exevc系统调用处理过程来理解Linux内核如何装载和启动一个可执行程序...
  3. [COCI 2013/2014 ROUND 3] parovi
  4. lvs+keepalived简单配置
  5. 金山网盾漏洞已第一时间修复、用户无风险
  6. 多线程编程(3) - 多线程同步之 CriticalSection(临界区)
  7. Hotpatch潜在的安全风险
  8. 用老司机的理财经历告诉你,这才是程序员理财的正确姿势
  9. ASP.NET MVC 防止 CSRF 的方法
  10. input:focus