XSLT
1. xsl
xsl(EXtensible Stylesheet Language) 是xml 的样式表语言,xml 不使用预先定义的标签,标签无确定意义,浏览器不知如何显示xml,这就需要xsl
CSS = HTML Style Sheets
html使用预先定义好的标签,这些标签是 well understood,如<table>定义了一个表格,浏览器知道如何显示,对html标签添加样式就比较容易,用css 就可以告诉浏览器用特定的font or color 显示元素
XSL = XML Style Sheets
xml 不使用预先定义的标签,这意味着这些标签是 not well understood,<table>可以表示 html table,也可是一件家具,或者其他的东西,这就需要xsl来告诉浏览器如何显示
xsl 包含3个部分:
XSLT - 转换xml文档的语言
XPath - 对xml文档导航的语言
XSL-FO - 格式化xml文档的语言
2. xslt
XSLT = XSL Transformations
xslt 一般用于把一个xml 文档转换成另一个xml 文档或html,用xslt 你可以向输出文件 add/remove 元素或属性,重新排序元素,确定显示哪些元素等,xslt用xpath定义源文档中与预定义模板相匹配的那些部分,找到后就将匹配部分转换成结果文档
3. 一个xsl 转换的例子
先来做一个例子,然后在却看它的语法
xml 文档 cdcatalog.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Atlantic</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>
XSL Style Sheet 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:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<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>
下面要做的就是把XSL Style Sheet 连接到xml 文档,在xml 文档的第二行添加:
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
点击cdcatalog.xml就可以看到转换后的样子了
4. The <xsl:template> Element
<xsl:template>
用来定义模板,match 属性把一个模板和一个xml元素联系起来,match 的值是一个xpath表达式,如 match="/" 定义了整个文档
来看一个简化的xslt 文件:
<?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>
因为xsl文件本身也是个xml文件,所以总是以xml的声明开始, <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet>
定义了这个文件是个XSLT style sheet文档(版本号和名字空间属性)
<xsl:template>
定义了一个模板,match="/"把这个模板和xml源文档联系起来
<xsl:template>
的内容就是html输出,这里只做了个框架,没做转换工作
5. The <xsl:value-of> Element
<xsl:value-of>
可以把xml元素的值提取出来放入输出流中
现在我们可以把上个xslt文件中的两个 <td>.</td>
改为:
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
select 属性的值是个xpath 的表达式,他像个文件系统的导航,“/”选择子目录
6. The <xsl:for-each> Element
通过上面的修改,我们只能得到一条记录,现在用 <xsl:for-each>
把他们都输出来, <xsl:for-each>
可以选出指定节点集中的每个xml元素
改为:
<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>
还可以在 select 中添加过滤条件呢,如 <xsl:for-each select="catalog/cd[artist='Kim Larsen']">
合法的运算符:
= (equal)
!= (not equal)
< less than
> greater than
7. The <xsl:if> Element
这个if语句很简单,看看他的语法:
<xsl:if test="expression">
...
...some output if the expression is true...
...
</xsl:if>
在例子中的 for-each 里加个 if 看看效果:
<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>
8. The <xsl:choose> Element
语法:
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
做个例子,当price 大于 10 时,就让"Artist"的底色变为粉红
<?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>
9. The <xsl:apply-templates> Element
<xsl:apply-templates>
为当前元素或当前元素的子节点申请模板,如果为其添加了select属性那他只有在子元素与select属性值相匹配时才执行
看下边的XSL style sheet:
<?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>
</xsl:template><xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
</xsl:template>
</xsl:stylesheet>
xsl(EXtensible Stylesheet Language) 是xml 的样式表语言,xml 不使用预先定义的标签,标签无确定意义,浏览器不知如何显示xml,这就需要xsl
CSS = HTML Style Sheets
html使用预先定义好的标签,这些标签是 well understood,如<table>定义了一个表格,浏览器知道如何显示,对html标签添加样式就比较容易,用css 就可以告诉浏览器用特定的font or color 显示元素
XSL = XML Style Sheets
xml 不使用预先定义的标签,这意味着这些标签是 not well understood,<table>可以表示 html table,也可是一件家具,或者其他的东西,这就需要xsl来告诉浏览器如何显示
xsl 包含3个部分:
XSLT - 转换xml文档的语言
XPath - 对xml文档导航的语言
XSL-FO - 格式化xml文档的语言
2. xslt
XSLT = XSL Transformations
xslt 一般用于把一个xml 文档转换成另一个xml 文档或html,用xslt 你可以向输出文件 add/remove 元素或属性,重新排序元素,确定显示哪些元素等,xslt用xpath定义源文档中与预定义模板相匹配的那些部分,找到后就将匹配部分转换成结果文档
3. 一个xsl 转换的例子
先来做一个例子,然后在却看它的语法
xml 文档 cdcatalog.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Atlantic</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>
XSL Style Sheet 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:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<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>
下面要做的就是把XSL Style Sheet 连接到xml 文档,在xml 文档的第二行添加:
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
点击cdcatalog.xml就可以看到转换后的样子了
4. The <xsl:template> Element
<xsl:template>
用来定义模板,match 属性把一个模板和一个xml元素联系起来,match 的值是一个xpath表达式,如 match="/" 定义了整个文档
来看一个简化的xslt 文件:
<?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>
因为xsl文件本身也是个xml文件,所以总是以xml的声明开始, <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet>
定义了这个文件是个XSLT style sheet文档(版本号和名字空间属性)
<xsl:template>
定义了一个模板,match="/"把这个模板和xml源文档联系起来
<xsl:template>
的内容就是html输出,这里只做了个框架,没做转换工作
5. The <xsl:value-of> Element
<xsl:value-of>
可以把xml元素的值提取出来放入输出流中
现在我们可以把上个xslt文件中的两个 <td>.</td>
改为:
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
select 属性的值是个xpath 的表达式,他像个文件系统的导航,“/”选择子目录
6. The <xsl:for-each> Element
通过上面的修改,我们只能得到一条记录,现在用 <xsl:for-each>
把他们都输出来, <xsl:for-each>
可以选出指定节点集中的每个xml元素
改为:
<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>
还可以在 select 中添加过滤条件呢,如 <xsl:for-each select="catalog/cd[artist='Kim Larsen']">
合法的运算符:
= (equal)
!= (not equal)
< less than
> greater than
7. The <xsl:if> Element
这个if语句很简单,看看他的语法:
<xsl:if test="expression">
...
...some output if the expression is true...
...
</xsl:if>
在例子中的 for-each 里加个 if 看看效果:
<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>
8. The <xsl:choose> Element
语法:
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
做个例子,当price 大于 10 时,就让"Artist"的底色变为粉红
<?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>
9. The <xsl:apply-templates> Element
<xsl:apply-templates>
为当前元素或当前元素的子节点申请模板,如果为其添加了select属性那他只有在子元素与select属性值相匹配时才执行
看下边的XSL style sheet:
<?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>
</xsl:template><xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
</xsl:template>
</xsl:stylesheet>
irini
2006-02-01 13:22:46
评论:0
阅读:308
引用:0
