Advanced XPath: Using XPath Functions

XPath Functions

  • Conversion: boolean, string, number
  • Contexts: deep-equal, last, position
  • DateTimes: current-dateTime, day-from-dateTime, months-from-duration, timezone-from-dateTime
  • Math: avg, ceiling, count, floor, max, min, round, sum
  • Logic: true, false, not
  • Nodes: lang, local-name, name, namespace-uri, text
  • Sequences: distinct-values, empty, index-of, subsequence
  • Strings: concat, contains, lower-case, matches, normalize-space, replace, starts-with, string-length, substring, substring-after, substring-before, tokenize, translate, upper-case

XPath contains numerous functions. Some of the ones I use most include:

Yes! There are more than this!

Conversion Functions

  • boolean(arg): returns true or false after evaluating the argument 
    //div[boolean(@n='2')]
  • string(arg): returns the string value of the argument
    //div[string(@n)='2']
  • number(arg): returns the numerical value of the argument
    //div[number(@n)=2]

Conversion functions items and turn them into something else.

Context Functions

  • deep-equal(param1,param2): returns true if param1 and param2 are fully equal at depth
    deep-equal(person, //person[@xml:id='abc'])
  • last(): returns the number of items in a nodeset thus providing the last one
    //div/sp[last()]
  • position(): returns index position of current node
    //div[position()=3]

Context functions tell you something about your context in the document

DateTime Functions

  • dateTime(date,time): Converts the arguments to a datetime
  • year-from-dateTime(datetime)    Returns an integer that represents the year
    • month-from-dateTime(datetime)
    • day-from-dateTime(datetime
    • hours-from-dateTime(datetime)
    • minutes-from-dateTime(datetime)
    • seconds-from-dateTime(datetime)    

DateTime functions enable you to manipulate dates

//date[month-from-dateTime(@when)='02']

Duration Functions

  • years-from-duration(datetimedur) Returns an integer that represents the years component
    • months-from-duration(datetimedur)    
    • days-from-duration(datetimedur)    
    • hours-from-duration(datetimedur)
    • minutes-from-duration(datetimedur)
    • seconds-from-duration(datetimedur)

Duration functions enable you to work with time as a duration

//date[days-from-duration(@dur)=3

Date and Time Functions

 

  • year-from-date(dateReturns an integer that represents the year
    • month-from-date(date)    
    • day-from-date(date)    
  • hours-from-time(time) Returns an integer that represents the hours
    • minutes-from-time(time)    
    • seconds-from-time(time)    

 

Date and Time functions enable you to work with dates that are not datetimes

//person[year-from-date(birth/@when)=1985]

Timezone Functions

  • timezone-from-dateTime(datetime)
    • timezone-from-date(date)
    • timezone-from-time(time)
  • adjust-dateTime-to-timezone(datetime,timezone)
    • adjust-date-to-timezone(date,timezone)    
    • adjust-time-to-timezone(time,timezone)    

Timezone functions enable you to get timezones from dates, or change dates to timezones

//date[timezone-from-date(@when)='-PT5H']

Numerical Functions

  • abs(num)   
    Returns the absolute value of the argument
  • ceiling(num)    
    Returns the smallest integer that is greater than the number argument
  • floor(num)    
    Returns the largest integer that is not greater than the number argument
  • round(num)    
    Rounds the number argument to the nearest integer
  • round-half-to-even(num)
    Rounds the number argument to the nearest even integer

Aggregate Functions

  • count(item,item,...)  Returns the count of nodes
  • avg(arg,arg,...)    Returns the average of the argument values
  • max(arg,arg,...)    Returns the argument that is greater than the others
  • min(arg,arg,...)    Returns the argument that is less than the others
  • sum(arg,arg,...   Returns the sum of the numeric value of each node in the specified node-set

 

Logic Functions

  • not(arg) The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true
  • true()    Returns the boolean value true
  • false()    Returns the boolean value false

//div[not(@type='act')]

Node Functions

  • name() Returns the name of the current or specified node
  • local-name() Returns the name of the current or specified node without the namespace prefix
  • namespace-uri() Returns the namespace URI of the current or specified node
  • lang(lang) Returns true if the language of the current node matches the language of the specified language
  • root() Returns the root of the tree to which the current node or the specified belongs. This will usually be a document node

 

//*[local-name()='date']

Sequence Functions

  • index-of((item,item,...),searchitem)  Returns the positions within the sequence of items that are equal to the searchitem argument
  • remove((item,item,...),position) Returns a new sequence constructed from the value of the item arguments - with the item specified by the position argument removed
  • empty(item,item,...)    Returns true if the value of the arguments IS an empty sequence, otherwise it returns false
  • exists(item,item,...   Returns true if the value of the arguments IS NOT an empty sequence, otherwise it returns false

 

index-of ((15, 40, 25, 10), 40) = 2

More Sequence Functions

  • distinct-values((item,item,...),collation)   Returns only distinct (different) values
  • insert-before((item,item,...),pos,inserts)   Returns a new sequence with the value of the inserts argument inserted in the position specified by the pos argument
  • reverse((item,item,...))    Returns the reversed order of the items specified
  • subsequence((item,item,...),start,len)    Returns a sequence of items from the position specified continuing for length specified. The first item is located at position 1
  • unordered((item,item,...))  Returns the items in an implementation dependent order

distinct-values(//*/@rend)

String Functions

  • string(arg) Returns the string value of the argument
  • codepoints-to-string((int,int,...)) Creates a string from a sequence of the Unicode code points
  • string-to-codepoints(string) Returns the sequence of the Unicode code points from a string
  • codepoint-equal(comp1,comp2) Returns true if the value of comp1 is Unicode equal to the value of comp2
  • compare(comp1,comp2) Returns -1 if comp1 is less than comp2, 0 if comp1 is equal to comp2, or 1 if comp1 is greater than comp2 

string(@when)

Substring Functions

  • substring(string,start,len) Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end
  • string-length(string) Returns the length of the specified string. If there is no string argument it returns the length of the string value of the current node
  • substring-before(string1,string2)    Returns the start of string1 before string2 occurs in it
  • substring-after(string1,string2)    Returns the remainder of string1 after string2 occurs in it

 

substring-before(substring-after(@when), '-'), '-')

Translation Functions

  • normalize-space(string) Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. If there is no string argument it does the same on the current node
  • upper-case(string) Converts the string argument to upper-case
  • lower-case(string) Converts the string argument to lower-case
  • translate(string1,string2,string3) Converts string1 by replacing the characters in string2 with the characters in string3

//div[lower-case(@type='chapter')]

String Testing Functions

  • contains(string1,string2)    Returns true if string1 contains string2, otherwise it returns false
  • starts-with(string1,string2)    Returns true if string1 starts with string2, otherwise it returns false
  • ends-with(string1,string2)    Returns true if string1 ends with string2, otherwise it returns false

//ref[not(starts-with(@target, 'http')]

String Testing Functions

  • concat(string,string,...)    Returns the concatenation of the strings
  • string-join((string,string,...),sep)    Returns a string created by concatenating the string arguments and using the sep argument as the separator
  • matches(string,pattern)    Returns true if the string argument matches the pattern, otherwise, it returns false
  • replace(string,pattern,replace)    Returns a string that is created by replacing the given pattern with the replace argument
  • tokenize(string,pattern)    Returns sequence from a string broken up by the pattern

tokenize(string(@when), '-')

Now let's try some of those out

Advanced XPath: Using XPath Functions

By James Cummings

Advanced XPath: Using XPath Functions

"Advanced XPath: Using XPath Functions" for pre-exist-db refresher course in Maynooth.

  • 2,973