Xpath Examples


XPath Examples





XPath examples for Google Spreadsheets



Note Minor Variations in Google Spreadsheets Xpath to Javascript Xpath and Other Internet Standards / languages :



Single Qoutes ' instead of Double Qoutes "
For attributes and specific entities: [eg. //*/H1(@class='Title') ]



Google Spreadsheets/JavaScript Variations


Xpath Examples
//*div
wildcard -any divs
//*div[@class='perfect']
wildcard -any divs with class 'perfect'


//td[@class='even' and position()=1]
every first td of 'even' class

//table[count(tr)=1 and count(tr/td)=2]
all tables with 1 row and 2 cols

//table/div/form/parent::*
all divs that have form
//table/div/form/ancestor::*
all tables that have form


./div/b
a relative path

//table[parent::div[@class='result'] and not(@id)]//
a any anchor in a table without id, contained in a div of 'result'

class /html/body/div/*[preceding-sibling::h4]
all nodes/attributes/elements after h4


//tr/td[font[@class='head' and text()='TRACK']]
all td that has font of a 'head' class and text 'TRACK'

./table/tr[last()]
the last row of a table

//rdf:Seq/rdf:li/em:id
using namespaces

//a/@href
hrefs of all anchors

//*[count(*)=3]
all nodes with 3 children
//*[count(*)<3]
all nodes with less than 3 children


//var |//acronym
all vars or acronym
//var and //acronym
all vars and acronym
//var and not //acronym
all vars and NOT acronym


XPath Functions



Conversion: boolean( [object] )
string( [object] )
number( [object] ) Math: ceiling( number )
floor( number )
round( decimal )
sum( node-set ) Logic: true()
false()
not( expr ) Node: lang(string)
name([node-set])
namespace-uri([node-set])
text() Context: count(node-set)
function-available( name )
last()
position()
String: contains( haystack-string
needle-string )
concat( string1
string2 [
stringn]* )
normalize-space( string )
starts-with(haystack
needle)
string-length( [string] )
substring(string
start [
length])
substring-after(haystack
needle)
substring-before(haystack
needle)
translate( string
abc
XYZ)

XPath Axes



ancestor
ancestor-or-self
attribute (@)
child (/)
descendant (//)
descendant-or-self
following
following-sibling
parent(..)
preceding
preceding-sibling
self (.)

Special Characters in regular expressions.


* wildcard - 0 or more + 1 or many ? 0 or 1 {n} exactly n {n,} n or more {n,m} n to m \ meta character for special chars, or "take literally" otherwise ^ begining; or negation in character set $ match end; EOL if multiline . any char (x) group; match & capture x (?:x) group; match x(dont capture) x(?=y) match x if followed by y x(?!y) match x if not followed by y x|y x or y [xyz] character set, match any char from xyz [^xyz] complemented character set [\b] backspace \b \B word boundary \cX control character X in a string \d \D digit = [0-9]; not digit \f form feed \n linefeed \r carriage return, \s \S single whitespace = [ \f\n\r\t\v\u00A0\u2028\u2029] \t tab \v vertical tab \w \W word char=alphanumeric char+_= [A-Za-z0-9_] \n group back reference (number, left count) \0 null \xhh \uhhhh hex code char


Xpath Resources



Xpath in Google Docs spreadsheet may vary slightly to Javascript and Other Standard Xpath eg (' vs ")

W3schools - Xpath Tutorial

XML Path Language (XPath)