The most perfect XSLT numeric function and string function

Source: Internet
Author: User
Tags xpath xsl xslt

Any programming language or SQL statement has built-in functions or methods, and so is the powerful and flexible XSLT technique. Mastering the use of XSLT's common functions, XSLT's application will become so easy that you will find that XSLT is more bull than you think! The following is a description and reference example of an XSLT numeric function with an XSLT string function.

1, the function of the XSLT value:
(1) Fn:number (ARG) returns the numeric value of the parameter. Parameters can be Boolean, string, or node set.
Example: <xsl:value-of select= "number ('") "/> Returns 100

(2) fn:abs (num) returns the absolute value of the parameter.
Example: <xsl:value-of select= "number ( -3.14)"/> Return 3.14

(3) fn:ceiling (num) returns the smallest integer greater than the num parameter. (Note: Some articles here are written incorrectly, will be greater than written less than)
Example: <xsl:value-of select= "ceiling (3.14)"/> Return 4

(4) Fn:floor (NUM) returns the largest integer less than or equal to the num parameter.
Example: <xsl:value-of select= "floor (3.14)"/> Return 3

(5) Fn:round (NUM) rounds the num parameter to the nearest integer.
Example: <xsl:value-of select= "round (3.14)"/> Return 3

(6) Round-half-to-even (num)
Example: <xsl:value-of select= "Round-half-to-even (0.5)"/> return 0
Example: <xsl:value-of select= "Round-half-to-even (1.5)"/> Return 2
Example: <xsl:value-of select= "Round-half-to-even (2.5)"/> Return 2


2. XSLT String functions
(1) fn:string (ARG) returns the string value of the parameter. Parameters can be numbers, logical values, or node sets.
Example: <xsl:value-of select= "string (314)"/> Returns "314"

(2) fn:codepoints-to-string (Int,int,...) returns a string based on a sequence of code points.
Example: <xsl:value-of select= "codepoints-to-string (+, 104, 233, 232, 101),/> return ' Thérèse '

(3) fn:string-to-codepoints (string) returns a sequence of code points based on a string.
Example: <xsl:value-of select= "string-to-codepoints (" Thérèse ")"/> Returns 84, 104, 233, 114, 232, 115, 101

(4) fn:codepoint-equal (COMP1,COMP2) returns True if the value of COMP1 equals the value of COMP2, as compared to the Unicode code point. (http://www.w3.org/2005/02/xpath-functions/collation/codepoint), otherwise returns false.
Example: <xsl:value-of select= "compare (' Ghi ', ' ghi ')"/> Returns True

Fn:compare (COMP1,COMP2)
Fn:compare (comp1,comp2,collation) returns 1 if the COMP1 is less than COMP2. If Comp1 equals COMP2, 0 is returned. If COMP1 is greater than COMP2, 1 is returned. (According to the control rules used).
Example: <xsl:value-of select= "compare (' Ghi ', ' ghi ')"/> return 0

(5) Fn:concat (string,string,...) returns the concatenation of the strings.
Example: <xsl:value-of select= "concat (' XPath ', ' is ', ' fun! ')" /> Return ' XPath is fun! '

(6) Fn:string-join ((string,string,...), Sep) uses the Sep parameter as a delimiter to return the string after concatenation of the string parameter.
Example: <xsl:value-of select= "String-join ((' Www. ', ' Mobansheji ', '. ', ' com '), ')"/> Return ' www.mobansheji.com '

(7) fn:substring (String,start,len)
Fn:substring (String,start) returns a substring of the specified length starting at the start position. The subscript for the first character is 1. If the Len argument is omitted, a substring from position start to the end of the string is returned.
Example: <xsl:value-of select= "substring (' www.mobansheji.com ', 1,4)"/> Return ' www. '
<xsl:value-of select= "substring (' www.mobansheji.com ', 4)"/> Return ' mobansheji.com '

(8) Fn:string-length (String) fn:string-length () returns the length of the specified string. If there is no string argument, the length of the string value of the current node is returned.
Example: <xsl:value-of select= "substring (' www.mobansheji.com ')"/> return 18

(9) Fn:normalize-space (String) fn:normalize-space () deletes whitespace at the beginning and end of the specified string and replaces all inner white space sequences with one, and then returns the result. If there is no string argument, the current node is processed:
Example: <xsl:value-of select= "Normalize-space (' www. Mobansheji. com ') "/> Return ' www.mobansheji.com '

Fn:upper-case (String) Converts the string argument to uppercase.
Example: <xsl:value-of select= "Upper-case (' the XPath ')"/> Returns ' the XPath '

(one) fn:lower-case (string) Converts the string argument to lowercase.
Example: <xsl:value-of select= "lower-case (' the xml ')"/> Returns ' the XML '

Fn:translate (STRING1,STRING2,STRING3) replaces string2 in string1 with String3.
Example: <xsl:value-of select= "translate (' Do you know XML and XPath? ', ' xml ', ' XSLT ') '/> Return ' Do you know XSLT and XPath? '
Example: <xsl:value-of select= "translate (' The ' 12:30pm ', ' A ', '") '/> Back ' 12:45 '

(Fn:escape-uri) (stringuri,esc-res)
Example: <xsl:value-of select= "Escape-uri (" Http://example.com/test#car ", True ())"/> Back "http%3a%2f%2fexample.com% 2ftest#car "
Example: <xsl:value-of select= "Escape-uri (" Http://example.com/test#car ", False ())"/> Back "Http://example.com/test#car"

Fn:contains (string1,string2) returns True if string1 contains string2, otherwise false.
Example: <xsl:value-of select= "contains (' XML ', ' XM ')"/> Returns True

Fn:starts-with (string1,string2) returns True if string1 starts with string2, otherwise false is returned.
Example: <xsl:value-of select= "Starts-with (' XML ', ' X ')"/> Returns True

(+) Fn:ends-with (string1,string2) returns True if string1 ends in string2, otherwise false.
Example: <xsl:value-of select= "Ends-with (' XML ', ' X ')"/> return False

Fn:substring-before (string1,string2) returns the substring before string2 appears in string1.
Example: <xsl:value-of select= "Substring-before (' www.mobansheji.com ', '. ')" /> return ' www '

Fn:substring-after (string1,string2) returns the substring after string2 appears in string1.
Example: <xsl:value-of select= "Substring-before (' www.mobansheji.com ', '. ')" /> return ' mobansheji.com '

(+) fn:matches (String,pattern) returns True if the string parameter matches the specified pattern, otherwise false.
Example: <xsl:value-of select= "matches (" Merano "," ran ")"/> Returns True

Fn:replace (String,pattern,replace) replaces the specified pattern with the Replace parameter and returns the result.
Example: <xsl:value-of select= "replace (" The password is admin888 "," 8 "," * ")"/> Back "the password is admin*** '

(fn:tokenize) (String,pattern)
Example: <xsl:value-of select= "Tokenize (" XPath is Fun "," \s+ ")"/> Return ("XPath", "is", "fun")


With the above XSLT numeric functions and XSLT String functions, I think all of you have learned how to use XSLT to handle various values and strings. In the actual application may be more complex than the sample code, only proficiency can be applied freely! I hope I can repeat it a few times more!

Format-number ()

The most perfect XSLT numeric function with a string function (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.