current()
函数
--------------------------------------------------------------------------------
定义与用法
current()
函数返回一个仅包含当前节点的节点集。通常当前节点和上下文节点是一样的。
<xsl:value-of select="current()"/>
与
<xsl:value-of select="."/>
是一样的。
然而,有一点不同。看下面的
XPath
表达式:
"catalog/cd"
。这个表达式选择
<catalog>
作为当前节点,然后选择
<catalog>
的子节点
<cd>
。这意味着在赋值的每一步,
"."
都有不同的意义。
下面这行代码:
将会处理所有
title
属性的值与当前节点的
ref
属性的值相等的所有
cd
元素。
<xsl:apply-templates select="//cd[@title=current()/@ref]"/>
这与下面有区别
下面这段代码将会处理所有
title
属性的值与
ref
属性的值相等的所有
cd
元素。
<xsl:apply-templates select="//cd[@title=./@ref]"/>
--------------------------------------------------------------------------------
语法
node-set current()
例子
1
<?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>
<xsl:for-each select="catalog/cd/artist">
Current node: <xsl:value-of select="current()"/>
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
document()
函数
--------------------------------------------------------------------------------
定义与用法
document()
函数用来访问外部的
XML
文档。外部
XML
文档必须是正确可解析的。
<xsl:value-of select="document('celsius.xml')/celsius/result[@value=$value]"/>
--------------------------------------------------------------------------------
语法
node-set document(object,node-set?)
参数
参数
|
说明
|
object
|
必须。定义
XML
文档的
URI
。
|
node-set
|
可选。用来处理相关的
URI
。
|
例子
1
Document.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="document.xsl" ?>
<groups>
<groupRef>hrGroup.xml</groupRef>
<groupRef>myGroup.xml</groupRef>
</groups>
document.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<groups>
<xsl:apply-templates select="/groups/groupRef"/>
</groups>
</xsl:template>
<xsl:template match="groups/groupRef">
<xsl:copy-of select="document(.)//group"/>
</xsl:template>
</xsl:stylesheet>
hrGroup.xml
<?xml version='1.0'?>
<group name="hr">
<leader>mo</leader>
<member>bo</member>
<member>ko</member>
<member>lo</member>
</group>
myGroup.xml
<?xml version='1.0'?>
<group name="my">
<leader>john</leader>
<member>jane</member>
<member>jon</member>
<member>jan</member>
</group>
例子
2
下面这个例子,显示如何使用两个参数。
第二个参数必须是一个节点集,作为第一个参数的基准
URI
,当没有第二个参数时,第一个参数的基准
URI
就是
XSL
的
URI
,如下,把
a.xml
放到
subdir
目录下,而另外两个在
../
,如果
document('a.xml', .)
的第二个参数不写,将以
../
作为
a.xml
的目录,就会报错,您可以实验一下。
document2.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:comment>One Argument </xsl:comment>
<xsl:for-each select="document('b.xml')//a">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:comment>Two Argument </xsl:comment>
<xsl:for-each select="document('a.xml', .)//a">
<xsl:copy-of select="."/>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Subdir/a.xml
<?xml-stylesheet type="text/xsl" href="../document2.xsl" ?>
<doc>
<a> I </a>
<a> II </a>
<a> III </a>
</doc>
b.xml
<doc>
<a> one </a>
<a> two </a>
<a> three </a>
</doc>
element-available()
函数
--------------------------------------------------------------------------------
定义与用法
element-available()
函数检查
XSLT
处理器是否能处理指定的元素。
这个函数只能用来检查在模板里出现的元素,这些元素是:
xsl:apply-imports
xsl:apply-templates
xsl:attributes
xsl:call-template
xsl:choose
xsl:comment
xsl:copy
xsl:copy-of
xsl:element
xsl:fallback
xsl:for-each
xsl:if
xsl:message
xsl:number
xsl:processing instruction
xsl:text
xsl:value-of
xsl:variable
--------------------------------------------------------------------------------
语法
boolean element-available(string)
参数
参数
|
说明
|
string
|
必须。指定要检查元素。
|
例子
1
<?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>
<xsl:choose>
<xsl:when test="element-available('xsl:comment')">
<p>xsl:comment is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>xsl:comment is not supported.</p>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="element-available('xsl:delete')">
<p>xsl:delete is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>xsl:delete is not supported.</p>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template></xsl:stylesheet>
format-number()
函数
--------------------------------------------------------------------------------
定义与用法
format-number()
函数用来格式化一个数字字符串。
--------------------------------------------------------------------------------
语法
string format-number(number,format,[decimalformat])
参数
参数
|
说明
|
number
|
Required. Specifies the number to be formatted
必须。指定要格式化的数字
|
format
|
必须。指定格式化的模式。
&S226;
# (
指示一个数字。
比如:
####)
&S226;
0 (Denotes leading and following zeros. Example: 0000.00)
&S226;
. (
指定小数点的位置。比如
: ###.##)
&S226;
, (The group separator for thousands. Example: ###,###.##)
&S226;
% (Displays the number as a percentage. Example: ##%)
&S226;
; (Pattern separator. The first pattern will be used for positive numbers and the second for negative numbers)
| |
decimalformat
|
可选。用来指定使用的
<xsl:decimal-format>
元素的名称。
|
例子
1
<?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>
<xsl:value-of select='format-number(500100, "#.00")' />
<br />
<xsl:value-of select='format-number(500100, "#.0")' />
<br />
<xsl:value-of select='format-number(500100, "###,###.00")' />
<br />
<xsl:value-of select='format-number(0.23456, "##%")' />
<br />
<xsl:value-of select='format-number(500100, "#######")' />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
function-available()
函数
--------------------------------------------------------------------------------
定义与用法
function-available()
函数检查指定的函数在
XSLT
处理器中是否支持。
You may test the XSLT functions and the inherited XPath functions.
您可以检查
XSLT
函数和
XPath
函数。
--------------------------------------------------------------------------------
语法
boolean function-available(string)
参数
参数
|
说明
|
string
|
必须。指定要检查的函数。
|
例子
1
<?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>
<xsl:choose>
<xsl:when test="function-available('sum')">
<p>sum() is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>sum() is not supported.</p>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="function-available('current')">
<p>current() is supported.</p>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
generate-id()
函数
--------------------------------------------------------------------------------
定义与用法
generate-id()
函数返回一个唯一标示指定节点的字符串。
如果指定的节点集是空的,将返回一个空字符串。如果您忽略了节点集的参数,默认是当前节点。
--------------------------------------------------------------------------------
语法
string generate-id(node-set?)
参数
参数
|
说明
|
node-set
|
可选。指定要生成唯一
id
的节点集
|
例子
1
<?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>
<h3>Artists:</h3>
<ul>
<xsl:for-each select="catalog/cd">
<li>
<a href="#{generate-id(artist)}">
<xsl:value-of select="artist" /></a>
</li>
</xsl:for-each>
</ul>
<hr />
<xsl:for-each select="catalog/cd">
Artist: <a name="{generate-id(artist)}">
<xsl:value-of select="artist" /></a>
<br />
Title: <xsl:value-of select="title" />
<br />
Price: <xsl:value-of select="price" />
<hr />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
key()
函数
--------------------------------------------------------------------------------
定义与用法
key()
函数使用
<xsl:key>
元素指定的索引返回文档中的一个节点集。
--------------------------------------------------------------------------------
语法
node-set key(string, object)
参数
参数
|
说明
|
string
|
必须。指定
xsl:key
元素的名称。
|
object
|
必须。要查找的字符串。
|
例子
1
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:key name="cdlist" match="cd" use="title" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('cdlist', 'Empire Burlesque')">
<p>
Title: <xsl:value-of select="title" />
<br />
Artist: <xsl:value-of select="artist" />
<br />
Price: <xsl:value-of select="price" />
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
system-property()
函数
--------------------------------------------------------------------------------
定义与用法
The system-property() function returns the value of the system property specified by the name.
system-property()
函数返回指定名称的系统属性。
System properties in the XSLT namespace:
在
XSLT
名称空间里的系统属性:
xsl:version -
指出
XSLT
的版本
xsl:vendor -
指出
XSLT
处理器
(
比如
James Clark)
xsl:vendor-url -
指出处理器的
URL(
比如
http://www.jclark.com/)
(注:当用
XT
解析时
xsl:vendor
是
James Clark
,
xsl:vendor-url
是
http://www.jclark.com/
,用
IE
就为
Microsoft,http://www.microsoft.com
)
--------------------------------------------------------------------------------
语法
object system-property(string)
参数
参数
|
说明
|
string
|
必须。指定返回的系统属性
|
例子
1
<?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>
<p>
Version:
<xsl:value-of select="system-property('xsl:version')" />
<br />
Vendor:
<xsl:value-of select="system-property('xsl:vendor')" />
<br />
Vendor URL:
<xsl:value-of select="system-property('xsl:vendor-url')" />
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
unparsed-entity-uri()
函数
--------------------------------------------------------------------------------
定义与用法
unparsed-entity-uri()
函数返回未解析的实体的
URI
。实体名称必须与传递的参数匹配。如果没有这样的实体,那么返回空串。
如果
DTD
包含下面的声明:
<!ENTITY pic SYSTEM "http://www.w3schools.com/picture.jpg" NDATA JPEG>
下面的表达式
unparsed-entity-uri('pic')
将会返回
"picture.jpg"
的
URI
。
--------------------------------------------------------------------------------
语法
string unparsed-entity-uri(string)
参数
参数
|
说明
|
string
|
必须。指定未解析实体
|
例子
1
XML
文件
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<!DOCTYPE catalog [
<!ELEMENT catalog ANY>
<!ELEMENT book ANY>
<!ELEMENT title ANY>
<!NOTATION JPEG SYSTEM "urn:foo">
<!ENTITY pic SYSTEM "http://www.w3schools.com/picture.jpg" NDATA JPEG>
]>
<catalog>
<book>
<title>XML Developer's Guide</title>
</book>
<book>
<title>Midnight Rain</title>
</book>
</catalog>
XSL
文件
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h3>unparsed-entity-uri()</h3>
<ul>
<li>
<b>unparsed-entity-uri('pic')</b> =
<xsl:value-of select="unparsed-entity-uri('pic')"/>
</li>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
运算符和特殊字符
!=
不等于
"" (literal)
文字
'' (literal)
文字
() (grouping)
分组
* (all nodes)
所有节点
* (multiplication)
通配符
+
加
-
减
- (unary minus)
. (self axis short form)
当前元素
.. (parent axis short form)
父元素
/ (step separator)
选择子元素
// (descendant-or-self axis short form)
选择子元素,不论多深
:: (axis specifier)
(不知道怎么翻译合适,没用过)
<
小于
<=
小于等于
=
等于
>
大于
>=
大于等于
@ (attribute axis short form)
选择属性
@* (all attributes)
选择所有属性
[] (predicate)
断言,指定过滤样式
And
与
axis nodetest predicate (step)
div The div operator performs floating-point division according to IEEE 754.
(原文放上来,不知道怎么翻译合适,没用过)
func() (function call)
调用函数
mod
取余
name (node test)
or
或
| (union)
集合
附录
如果没有特殊说明,将使用下面的
XML
文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<!—
在这里引入您的
XSL
文件
-->
<?xml-stylesheet type="text/xsl" href="cdcatalog_element.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>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
<company>Mega</company>
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
</cd>
<cd>
<title>Big Willie style</title>
<artist>Will Smith</artist>
<country>USA</country>
<company>Columbia</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
<company>Polydor</company>
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<title>Soulsville</title>
<artist>Jorn Hoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<title>Stop</title>
<artist>Sam Brown</artist>
<country>UK</country>
<company>A and M</company>
<price>8.90</price>
<year>1988</year>
</cd>
<cd>
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
<company>Siren</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Private Dancer</title>
<artist>Tina Turner</artist>
<country>UK</country>
<company>Capitol</company>
<price>8.90</price>
<year>1983</year>
</cd>
<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>