xsl 函数大全

XSLT supplies a number of functions.

 


--------------------------------------------------------------------------------

XSLT Functions
Name Description 
current() Returns the current node 
document() Used to access the nodes in an external XML document 
element-available() Tests whether the element specified is supported by the XSLT processor 
format-number() Converts a number into a string 
function-available() Tests whether the function specified is supported by the XSLT processor 
generate-id() Returns a string value that uniquely identifies a specified node 
key() Returns a node-set using the index specified by an element 
system-property() Returns the value of the system properties 
unparsed-entity-uri() Returns the URI of an unparsed entity 


--------------------------------------------------------------------------------

Inherited XPath Functions
Node Set Functions
Name Description Syntax 
count() 返回结点个数 number=count(node-set) 
id() Selects elements by their unique ID node-set=id(value) 
last() 返回最后一个结点的需要 number=last() 
local-name() Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name string=local-name(node) 
name() 返回一个结点的名称 string=name(node) 
namespace-uri() 返回一个结点的名称空间 uri=namespace-uri(node) 
position() 返回结点序号 number=position() 

String Functions
Name Description Syntax & Example 
concat() 返回一个将N个字符串连接的值 string=concat(val1, val2, ..) 
示例:
concat('The',' ','XML')
返回结果: 'The XML'
 
contains() 如果val包含substr则返回true bool=contains(val,substr)

示例:
contains('XML','X')
返回结果: true
 
normalize-space() 删除多余空格 string=normalize-space(string) 
示例:
normalize-space(' The   XML ')
返回结果: 'The XML'
 
starts-with() 如果String以substr开始则返回true bool=starts-with(string,substr) 
示例:
starts-with('XML','X')
返回结果: true
 
string() 将数字转换为字符 string(value) 
示例:
string(314)
返回结果: '314'
 
string-length() 返回一个字符串的长度 number=string-length(string) 
示例:
string-length('Beatles')
返回结果: 7
 
substring() 将string从start开始一直截取到第length位 string=substring(string,start,length) 
示例:
substring('Beatles',1,4)
返回结果: 'Beat'
 
substring-after() 将string以substr分割,选取最后一个 string=substring-after(string,substr)

示例:
substring-after('12/10','/')
返回结果: '10'
 
substring-before() 将string以substr分割,选取第一个 string=substring-before(string,substr)

示例:
substring-before('12/10','/')
返回结果: '12'
 
translate() 对字串进行替换操作 string=translate(value,string1,string2) 
示例:
translate('12:30',':','!')
返回结果: '12!30'
 

Number Functions
Name Description Syntax & Example 
ceiling() 返回一个整数 number=ceiling(number) 
示例:
ceiling(3.14)
返回结果: 4
 
floor() 返回一个整数 number=floor(number)

示例:
floor(3.14)
返回结果: 3
 
number() 将字符型转为数字 number=number(value)

示例:
number('100')
返回结果: 100
 
round() 返回一个实数的四舍五入的整数值 integer=round(number) 
示例:
round(3.14)
返回结果: 3
 
sum() 返回结点值的和 number=sum(nodeset) 
示例:
sum(/cd/price)
 

Boolean Functions
Name Description Syntax & Example 
boolean() Converts the value argument to Boolean and returns true or false bool=boolean(value) 
false() 返回false false() 
示例:
number(false())
返回结果: 0
 
lang() Returns true if the language argument matches the language of the the xsl:lang element, otherwise it returns false bool=lang(language) 
not() Returns true if the condition argument is false, and false if the condition argument is true bool=not(condition) 
示例:
not(false())
 
true() Returns true true() 
示例:
number(true())
返回结果: 1
 


--------------------------------------------------------------------------------

The current() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.


is equal to


However, there is one difference. Look at the following XPath expression: 'catalog/cd'. This expression selects the child nodes of the current node, and then it selects the child nodes of the nodes. This means that on each step of evaluation, the '.' has a different meaning.

The following line:


will process all cd elements that have a title attribute with value equal to the value of the current node's ref attribute.

This is different from


that will process all cd elements that have a title attribute and a ref attribute with the same value.


--------------------------------------------------------------------------------

Syntax
node-set current()
 

Example 1

 

  
  
  
    Current node: 
    
  
  
  


 

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result in Netscape 6 or IE 6

The document() Function
 


--------------------------------------------------------------------------------

Definition and Usage
The document() function is used to access nodes in an external XML document. The external XML document must be valid and parsable.

One way to use this function is to look up data in an external document. For example we want to find the Celsius value from a Fahrenheit value and we refer to a document that contains some pre-computed results:

 

--------------------------------------------------------------------------------

Syntax
node-set document(object,node-set?)
 


Parameters
Parameter Description 
object Required. Defines an URI to an external XML document 
node-set Optional. Used to resolve relative URI 

 


--------------------------------------------------------------------------------

The element-available() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The element-available() function returns a Boolean value that indicates whether the element specified is supported by the XSLT processor.

This function can only be used to test elements that can occur in a template body. These elements are: 

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 

--------------------------------------------------------------------------------

Syntax
boolean element-available(string)
 

Parameters
Parameter Description 
string Required. Specifies the element to test 

Example 1

 

 

 

xsl:comment is supported.


xsl:comment is not supported.

 


xsl:delete is supported.


xsl:delete is not supported.

 

 


 

If you have Netscape 6 or IE 5 or higher you can view: the XSL file.

View the result in Netscape 6 or IE 6

 

--------------------------------------------------------------------------------

The format-number() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The format-number() function is used to convert a number into a string.


--------------------------------------------------------------------------------

Syntax
string format-number(number,format,[decimalformat])
 

Parameters
Parameter Description 
number Required. Specifies the number to be formatted 
format Required. Specifies the format pattern. Here are some of the characters used in the formatting pattern: 
# (Denotes a digit. 示例: ####) 
0 (Denotes leading and following zeros. 示例: 0000.00) 
. (The position of the decimal point 示例: ###.##) 
, (The group separator for thousands. 示例: ###,###.##) 
% (Displays the number as a percentage. 示例: ##%) 
; (Pattern separator. The first pattern will be used for positive numbers and the second for negative numbers) 
 
decimalformat Optional. 

Example 1

 

 

 

 

 

 

 

 

 

 

If you have Netscape 6 or IE 5 or higher you can view: the XSL file.

View the result in Netscape 6 or IE 6


The function-available() Function
 


--------------------------------------------------------------------------------

Definition and Usage
The function-available() function returns a Boolean value that indicates whether the function specified is supported by the XSLT processor.

You may test the XSLT functions and the inherited XPath functions.


--------------------------------------------------------------------------------

Syntax
boolean function-available(string)
 


Parameters
Parameter Description 
string Required. Specifies the function to test 


Example 1

 

 

 

sum() is supported.


sum() is not supported.

 


current() is supported.


current() is not supported.

 

 


 


If you have Netscape 6 or IE 5 or higher you can view: the XSL file.

View the result in Netscape 6 or IE 6

 

--------------------------------------------------------------------------------

The generate-id() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The generate-id() function returns a string value that uniquely identifies a specified node.

If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter, it defaults to the current node.


--------------------------------------------------------------------------------

Syntax
string generate-id(node-set?)
 

Parameters
Parameter Description 
node-set Optional. Specifies on which node-set to generate a unique id  

Example 1

 

 

Artists:

 

 

 


--------------------------------------------------------------------------------

Artist: 


Title: 

Price: 
--------------------------------------------------------------------------------

 

 

 

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result in Netscape 6 or IE 6

 

--------------------------------------------------------------------------------

The key() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The key() function returns a node-set from the document, using the index specified by an element.


--------------------------------------------------------------------------------

Syntax
node-set key(string, object)
 

Parameters
Parameter Description 
string Required. Specifies the name of an xsl:key element 
object Required. A string to search for 

Example 1

 

 

 


  
  Title: 
  
  Artist: 
  
  Price: 
  

 

 

 

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result in Netscape 6 or IE 6

 

--------------------------------------------------------------------------------

The system-property() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The system-property() function returns the value of the system property specified by the name.

System properties in the XSLT namespace:

xsl:version - The version of XSLT implemented by the processor 
xsl:vendor - The vendor of the XSLT processor 
xsl:vendor-url - The URL identifying the vendor of the XSLT processor 

--------------------------------------------------------------------------------

Syntax
object system-property(string)
 

Parameters
Parameter Description 
string Required. Specifies the system property to return the value of  

Example 1

 

 


Version:


Vendor:


Vendor URL:

 

 


 

If you have Netscape 6 or IE 5 or higher you can view: the XSL file.

View the result in Netscape 6 or IE 6

 

--------------------------------------------------------------------------------

The unparsed-entity-uri() Function
 
--------------------------------------------------------------------------------

Definition and Usage
The unparsed-entity-uri() function returns the URI of an unparsed entity. The name of the entity must match the passed argument. If there is no such entity an empty string is returned.

If the DTD contains the following declaration:


the following expression:

unparsed-entity-uri('pic')

will return the URI for the file 'picture.jpg'.


--------------------------------------------------------------------------------

Syntax
string unparsed-entity-uri(string)
 

Parameters
Parameter Description 
string Required. Specifies the name of an unparsed entity 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值