[XML] XPath – Expression and Functions

XPath is not just pattern matches to the path to the node. XPath uses path expressions to select nodes or node-sets in an XML document.

 

1. Expressions

The complete XPath expression language contains

  • patterns
  • location paths
  • functions
  • operators
  • and values of various types

 

2. Values

  • boolean: ‘true’ or ‘false’
  • number : any positive or negative real number
  • string : a sequence of characters
  • node-set : the string value of nodes selected using a location path

 

3. Variables

A variable may be used to represent a value of any type. Variable names are prefixed with a $ symbol.

$age

 

4. Operators

Operators are used to perform

  • simple calculations : +, -, *, div, mod
  • comparing values : <, >, <=, >=, !=, =
  • combining expressions : and, or
$age != 20
$age > 15 and $age < 18

 

5. Sample XML

<?xml version="1.0" ?>
<book>
  <head>
    <title>Lord of the Rings</title>
    <author gender="male">J.R.R. Tolkien</author>
    <date>
      <mm>03</mm>
      <dd>20</dd>
      <yy>2012
    </date>
  </head>
  <content>Hobbits</content>
  <content>wizards</content>
  <content>Sauron!</content>
</book>

 

6. Node Set Functions

  • last(): returns the index of the last node in the context list
  • position(): returns the position of the current node among the current list of nodes
  • count(node-set): returns the total number of nodes passed to it
  • id(object): returns the element node with the given unique identifier. Unique identifiers are only recognized if a DTD or schema is used and this DTD assigns one or more attributes to the ID attribute type
  • name(node-set?): returns the string value of the name of the given node
  • local-name(node-set?): returns the local name of the first node in the node set
  • namespace-uri(node-set?): returns the namespace URI of the first node in the node set.
<xsl:template match="content[position()=1]">
  content1 = <xsl:value-of select="text()" />
</xsl:template>

 

<xsl:template match="head">
  count1 = <xsl:value-of select="count(child::date)" />
  year = <xsl:value-of select="descendant::yy" />
</xsl:template>

 

<xsl:template match="date">
  nameOfElement1 = <xsl:value-of select="name(..)" />
</xsl:template>

The result will be

content1 = Lord of the Rings
count1 = 1
year = 2012

nameOfElement1 = head

 

6. String Functions

  • string(Object?): converts an object to a string
  • concat(string1, string2, string3*): returns the concatenation of its arguments
  • starts-with(string1, string2): returns true if string1 starts with string2
  • contains(string1, string2): returns true if string1 contains string2
  • 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(string, number, number?): returns the substring of the first argument starting at the position(second argument) with length(third argument)
  • string-length(string?): the number of characters in the string
  • translate(string1, string2, string3): converts string1 by replacing the characters in string2 with the characters in string3   Example (1) – Converting to string : string()
  • for a node set : returns the string-value of the node in the node-set that is first in document order
  • for a number : the number is converted to a string (‘NaN’, ‘Infinity’, ‘-Infinity’)
  • for a boolean : ‘true’ or ‘false’
<xsl:template match="title">
  string() = <xsl:value-of select="string()" />
  string(-35.3) = <xsl:value-of select="string(-35.3)" />
  string(-0) = <xsl:value-of select="string(-0)" />
</xsl:template>

 

string() = Lord of the Rings
string(-35.3) = -35.3
string(-0) = 0

Example (2) – String Analysis: string-length(), contains(), starts-with()

<xsl:template match="title">
  length_of_title = <xsl:value-of select="string-length(string())" />
  <xsl:if test="contains(string(),'Rings')"> The title contains 'Rings' </xsl:if>
  <xsl:if test="starts-with(string(),'Lord')"> The title starts with the 'Load' </xsl:if>
</xsl:template>

 

length_of_title = 17
The title contains 'Rings'
The title starts with the 'Load'

Example (3) – String Conversion : substring()

The position of the first character is 1, the position of the second character is 2 and so on. This differs from other programming languages and ECMAScript, in which the String.substring method treats the position of the first character as 0.

<xsl:template match="title">
  substring1 = <xsl:value-of select="substring(string(),9,3)" />
  substring2 = <xsl:value-of select="substring(string(),13)" />
  substring3 = <xsl:value-of select="substring-before(string(),'of')" />
  substring4 = <xsl:value-of select="substring-after(string(),'of')" />
</xsl:template>

 

substring1 = the
substring2 = Rings
substring3 = Lord
substring4 = the Rings

Example (4) – String Conversion : translate()

<xsl:template match="title">
  original = <xsl:value-of select="text()" />
  translation1 = <xsl:value-of select="translate(text(),'aeiou','AEIOU')" />
  translation2 = <xsl:value-of select="translate(text(),'LR','lr')" />
</xsl:template>

 

original = Lord of the Rings
translation1 = LOrd Of thE RIngs
translation2 = lord of the rings

 

7. Boolean Functions

  • boolean(Object): converts its argument to a boolean
  • not(boolean): returns true if its argument is false, and false otherwise
  • true(): returns true
  • false(): returns false

Example (1) – Converting to boolean : boolean()

The boolean() function converts its argument to a boolean as follows:

  • for a number : true if and only if it is neither zero nor NaN
  • for a node-set : true if and only if it is non-empty
  • for a string : true if and only if its length is non-zero
<xsl:template match="title">
  test1 = <xsl:value-of select="boolean(node())" />
  test2 = <xsl:value-of select="boolean(child::author)" />
  test3 = <xsl:value-of select="boolean(32)" />
  test4 = <xsl:value-of select="boolean(0)" />
  test5 = <xsl:value-of select="boolean(false())" />
</xsl:template>

 

test1 = true
test2 = false
test3 = true
test4 = false
test5 = flase

Example (2) – not(), true(), flase()

<xsl:template match="title">
  test1 = <xsl:value-of select="not(attribute::gender)" />
  test2 = <xsl:value-of select="not(true())" />
  test3 = <xsl:value-of select="not(false())" />
</xsl:template>

 

test1 = false
test2 = false
test3 = true

 

8. Number Functions

  • number(Object): converts its argument to a number
  • sum(node-set): returns the sum, for each node in the argument node-set, of the result of converting the string-values of the node to a number
  • floor(number): returns the largest integer number that is not greater than the argument
  • ceiling(number): returns the smallest integer number that is not less than the argument
  • round(number): returns the integer number that is closest to the argument

Example (1) – Converting to number : number()

The number() function converts its argument to a number as follows:

  • for a boolean : true->1 , false->0
  • for a string : the nearest mathematical value represented by the string; any other string is converted to NaN
  • for a node-set : first converted to a string as if by a call to the string function and then converted in the same way as a string argument
<xsl:template match="title">
  number1 = <xsl:value-of select="number(true())" />
  number2 = <xsl:value-of select="number(false())" />
  month = <xsl:value-of select="number(child::mm)" />
  badNumber = <xsl:value-of select="number('34Bad')" />
</xsl:template>

 

number1 = 1
number2 = 0
month = 3
badNumber = NaN

Example (2) – sum()

<xsl:template match="title">
  dd = <xsl:value-of select="number(child::dd)" />
  mm = <xsl:value-of select="number(child::mm)" />
  yy = <xsl:value-of select="number(child::yy)" />
  dd+mm+yy = <xsl:value-of select="sum(child::*)" />
</xsl:template>

 

dd = 20
mm = 3
yy = 2012
dd+mm+yy = 2035

Example (3) – Rounding values

<xsl:template match="title">
  floor = <xsl:value-of select="floor(0.5)" />
  ceiling = <xsl:value-of select="ceiling(0.5)" />
  round1 = <xsl:value-of select="round(0.5)" />
  round2 = <xsl:value-of select="round(0.4)" />
</xsl:template>

 

floor = 0
ceiling = 1
round1 = 1
round2 = 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s