Query language for XML

XPath

  • Consists of node (=element) and attribute
  • uses and, or for conditionals (not |, &, etc.)
  • {xpath}/EMPS/EMP/PHONE[@type] checks for the existence of an attribute
  • /bibliography/book/@ISBN returns all attribute values (ISBNs)
  • functions:
    • {xquery}x+y,x–y,x*y,x div y,x mod y
    • {xquery}contains(a,b): a contains b
    • {xquery}count($nodeset): number of child nodes in node set
    • {xquery}position(): n-th node
    • Regular Expressions matching {xquery}matches($string, $regex) e.g. {xquery}matches($input, 'H.*o W.*d')
  • conditionals:
    • {xquery}x=y note that one equals is fine, {xquery}x!=y
    • {xquery}x>y, {xquery}x < y
    • {xquery}and or not() works. (but &,|,! does not!)
  • Access parent node as {xquery}child/.. without trailing backslash
  • Accessing Attributes: {xquery}data($person/@name)
  • Frequently used in conditions:
    • {xquery}x + y
    • {xquery}x - y
    • {xquery}x * y
    • {xquery}x div y
    • {xquery}x mod y
  • functions
    • {xquery}contains(x, y): true if string x contains string y
    • {xquery}count(node-set): counts the number of nodes in node-set
    • {xquery}position(): returns the “context position” (roughly, the position of the current node in the node-set containing it)
    • {xquery}last(): returns the size of the node-set containing the current node
    • {xquery}name(): returns the tag name of the current element
    • {xquery}sum(): returns sum of all matches

XQuery

Standard format:

<result>{
	for $var in XPATH-QUERY (: Comments :)
	let $var2 ...
	where [condition]
	stable order by,
		$variable ascending,
		$variable descending
	return <elem>{xquery}</elem>
}</result>

Sort

Conditionals

Axis Test

  • /: shorthand for child-or-self
  • //: shorthand for descendant-or-self
  • /..: shorthand for parent
  • one of self, attribute,
  • parent, child, ancestor,† ancestor-or-self,† descendant, descendant-or-self,
  • following, following-sibling, preceding,† preceding-sibling,†
  • namespace
  • †: These are reverse axes (=produce resulting node-sets in reverse document order)
  • Use like:

Existential Conditions (Exists some…)

Date Operations

  • Dates will be printed as P dD T hH iM sS
    • P just a P.
    • d: Number of Days. D: constant
    • T: constant
    • h: Number of Hours, H: constant
    • i: Number of Minutes, M: is constant
    • s: Number of Seconds S: is constant
  • Dates can be
    • subtracted: {xquery}xs:date("1933-06-22") - xs:date("2000-01-01")
    • compared: {xquery}xs:date("1933-06-22") >= xs:date("2000-01-01")

Date functions

year-from-dateTimeday-from-dateTimeminutes-from-dateTime
year-from-dateday-from-dateminutes-from-time
years-from-durationminutes-from-durationdays-from-duration
month-from-dateTimehours-from-dateTimeseconds-from-dateTime
month-from-datehours-from-timeseconds-from-time
months-from-durationhours-from-durationseconds-from-duration

Tips and Tricks

Naming elements using variables: