You can use XML Path Language (XPath) functions to refine XPath queries and enhance the programming power and flexibility of XPath.
The functions are divided into the following groups: Node Set, String, Boolean and Number
Node Set Functions
Node-set functions take a node-set argument. They return a node-set, or information about a particular node within a node-set.
Function: [number] last() – Returns a number equal to context size of the expression evaluation context.
- Example: //book[last()] – Result: Selects the last book element
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256083(v=vs.100)
Function: [number] position() – Returns the position, or index number, of the node, relative to all the selected nodes in the node list.
- Example: //book[position()<=3] – Result: Selects the first three book elements
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256233(v=vs.100)
Function: [number] count(node-set) – Returns the number of nodes in the node-set argument.
- Example: <xsl:value-of select=”count(//book)”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256103(v=vs.100)
Function: [node-set] id(object) – Selects elements by their unique ID.
- Example: <xsl:value-of select=”id(‘0-672-32598-5’)”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256148(v=vs.100)
Function: [string] local-name(node-set?) – Returns the local part of the expanded name of the node in the node-set argument that is first in document order.
- Example: <xsl:value-of select=”local-name()”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256070(v=vs.100)
Function: [string] namespace-uri(node-set?) – Returns the namespace Uniform Resource Identifier (URI) of the expanded name of the node in the node-set argument that is first in document order.
Function: [string] name(node-set?) – Returns a string containing a QName representing the expanded name of the node in the node-set argument that is first in document order.
String Functions
String functions are used to evaluate, format, and manipulate string arguments, or to convert an object to a string.
Function: [string] string(object?) – Converts an object to a string.
- Example: string(/book/artist/text())
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256133(v=vs.100)
Function: [string] concat(string, string, string*) – Returns the concatenation of the arguments.
- Example: concat(/book/title, ” – “, /book/category)
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256123(v=vs.100)
Function: [boolean] starts-with(string, string) – Returns true if the first argument string starts with the second argument string; otherwise returns false.
- Example: <xsl:if test=”starts-with(/book/title, ‘W’)”>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256174(v=vs.100)
Function: [boolean] contains(string, string) – Checks whether the first argument string contains the second argument string.
- Example: <xsl:if test=”contains(/book/title, ‘BizTalk’)”>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256195(v=vs.100)
Function: [string] substring-before(string, string) – Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string.
- Example: <xsl:value-of select=”substring-before(/book/date, “/”)”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256071(v=vs.100)
Function: [string] substring-after(string, string) – Returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string.
- Example: <xsl:value-of select=”substring-after(/book/date, “/”)”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256455(v=vs.100)
Function: [string] substring(string, number, number?) – Returns the substring of the first argument starting at the position specified in the second argument and the length specified in the third argument.
- Example: substring(“12345”, 2, 3) returns “234”
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256054(v=vs.100)
Function: [number] string-length(string?) – Returns the number of characters in the string.
- Example: string-length(“BizTalk”) returns 7
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256171(v=vs.100)
Function: [string] normalize-space(string?) – Returns the argument string with the leading, trailing, and repeating white spaces stripped.
- Example: normalize-space(” working with XPath “) returns “working with XPath”
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256063(v=vs.100)
Function: [string] translate(string, string, string) – Returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string.
- Example: translate(’12:30′,’30’,’45’) Result: ’12:45′
- Example: translate(’12:30′,’03’,’54’) Result: ’12:45′
- Example: translate(’12:30′,’0123′,’abcd’) Result: ‘bc:da’
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256119(v=vs.100)
Boolean Functions
The XML Path Language (XPath) syntax supports Boolean functions that return strings or numbers, and can be used with comparison operators in filter patterns.
Function: [boolean] boolean(object) – Converts the argument to a Boolean.
- Example: boolean(NaN) Result false
- Example: boolean(‘hello’) Result true
- Example: boolean(”) Result false
- Example: boolean(//book) Result false
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256159(v=vs.100)
Function: [boolean] not(object) – Returns true if the argument is false; otherwise false.
- Example: <xsl:for-each select=”//book[not(price > 10)]”>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256458(v=vs.100)
Function: [boolean] true() – Returns true.
- Example: true() Result: true
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256486(v=vs.100)
Function: [boolean] false() – Returns false.
- Example: <xsl:value-of select=”false()”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256226(v=vs.100)
Function: [boolean] lang(string) – Returns true if the xml:lang attribute of the context node is the same as a sublanguage of the language specified by the argument string.
- Example: Lang(“en”) is true for <p xml:lang=”en”>…</p>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256192(v=vs.100)
Number Functions
The XML Path Language (XPath) syntax supports Number functions that return strings or numbers and can be used with comparison operators in filter patterns.
Function: [number] number(object?) – Converts the argument to a number.
- Example: <xsl:value-of select=”number(/book/id)”/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256211(v=vs.100)
Function: [number] sum(node-set) – Returns the sum of all nodes in the node-set. Each node is first converted to a number value before summing.
- Example: <xsl:value-of select=’sum(//book/price)’/>
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256160(v=vs.100)
Function: [number] floor(number) – Returns the largest integer that is not greater than the argument.
- Example: <xsl:value-of select=’floor(3.5)’/> Result: 3
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256464(v=vs.100)
Function: [number] ceiling(number) – Returns the smallest integer that is not less than the argument.
- Example: <xsl:value-of select=’ceiling(3.5)’/> Result: 4
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256087(v=vs.100)
Function: [number] round(number) – Returns an integer closest in value to the argument.
- Example: xsl:value-of select=’round(2.6)’/> Result 3
- Example: xsl:value-of select=’round(2.4)’/> Result 2
- Ref: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256213(v=vs.100)
thanks for the document very helpful