{ Frameless }

JavaScript operations and their XPath counterparts

Follow

If you know JavaScript well and just started using XPath for finding and filtering data, here's a mapping of various JavaScript operations to their XPath counterparts. If there's anything missing here contact us or send out a tweet to @frameless_io.

We're working on such an overview for CSS selectors as well.

Booleans

JavaScript XPath
true true()
false false()
!something not($something)
!!something boolean($something)

Type conversions

JavaScript XPath
Boolean("true") boolean("true")
String(42) string(42)
Number("42") number("42")
obj.toString() string($obj)

Math

JavaScript XPath
Math.min(x, y) min(($x, $y))
Math.min(x, Math.min(y, z)) min(($x, $y, $z))
Math.max(x, y) max(($x, $y))
Math.abs(x) abs($x)
Math.ceil(x) ceiling($x)
Math.floor(x) floor($x)
Math.round(x) round($x)
Math.exp(x) exp($x)
Math.pow(x, y) pow($x, $y)
Math.log(x) log($x)
Math.sqrt(x) sqrt($x)
Math.sin(x) sin($x)
Math.cos(x) cos($x)
Math.tan(x) tan($x)
Math.asin(x) asin($x)
Math.acos(x) acos($x)
Math.atan(x) atan($x)
Math.atan2(x, y) atan2($x, $y)

Arithmetic

JavaScript XPath
1 + 1 1 + 1
1 - 1 1 - 1
1 * 1 1 * 1
1 / 1 1 div 1
1 % 2 1 mod 2
Infinity INF
-Infinity -INF
NaN NaN
-0 -0

Strings

JavaScript XPath
"Hello " + $world "Hello " || $world
str.length string-length($str)
str.charCodeAt(0) string-to-codepoints($str)[1]
list.join('-') string-join($list, '-')
str.replace(pattern, x) replace($str, $pattern, $x)
str.toUpperCase() upper-case($str)
str.toLowerCase() lower-case($str)
str.indexOf(x) !== -1 contains($str, $x)
str.substring(...) starts-with($str, $x),

ends-with($str, $x),

substring-before($str, $x),

substring-after($str, $x)
str.split(',') tokenize($str, $pattern)

DOM methods and properties

JavaScript XPath
nodeA === nodeB $nodeA is $nodeB
a.parentNode === b $b[$a]
getElementById("nav") id('nav')
getElementsByTagName("*") //*
getElementsByTagName("p") //p
getElementsByTagNameNS("http://www.w3.org/2000/svg", "*") //svg:*
getElementsByTagNameNS("http://www.w3.org/2000/svg", "rect") //svg:rect
Node.childNodes node()
Node.childNodes.length count(node())
Node.firstElementChild *[1]
Node.lastElementChild *[last()]
Node.childElementCount count(*)
Node.previousElementSibling preceding-sibling::*[1]
Node.nextElementSibling following-sibling::*[1]
Node.parentElement parent::*
Node.firstChild node()[1]
Node.lastChild node()[last()]
Node.nextSibling following-sibling::node()[1]
Node.previousSibling preceding-sibling::node()[1]
Node.textContent string(.)
Node.ownerDocument /

root($node) (for connected nodes)
Node.parentNode ..
Node.hasChildNodes() node() or exists(node())
Document.URL base-uri(/)
Node.baseURI base-uri()
Node.lookupPrefix lookup-prefix()
Node.lookupNamespaceURI('svg') namespace-uri-for-prefix('svg')
Node.isEqualNode($b) deep-equals($a, $b)
Node.nodeName node-name()
Node.nodeType === Node.ELEMENT_NODE self::*
Node.nodeType === Node.TEXT_NODE ||

Node.nodeType === Node.CDATA_SECTION_NODE
self::text()
Node.nodeType === Node.PROCESSING_INSTRUCTION_NODE self::processing-instruction()
Node.nodeType === Node.COMMENT_NODE self::comment()
Node.nodeType === Node.DOCUMENT_NODE self::document-node()
Element.localName local-name()
Element.namespaceURI namespace-uri()
Element.id @id or @xml:id
Element.prefix prefix()
Element.tagName name()
Element.attributes @*
Element.getAttribute("href") @href
Element.getAttributeNS("http://www.w3.org/1999/xlink", "href") @xlink:href
Element.matches('p') self::p
Element.matches('a[href]') self::a[@href]
Element.query('form') .//form[1]
Element.queryAll('form') .//form
Attr.name name(@href)
Attr.prefix prefix(@xlink:href)
Attr.localName local-name(@xlink:href)
Attr.namespaceURI namespace-uri(@xlink:href)
Attr.value string(@href)
Attr.ownerElement ..

Date

JavaScript XPath
new Date current-dateTime()
date.getDate() day-from-dateTime($date) Day of month (e.g: 1 - 31)
date.getFullYear() year-from-dateTime($date) Year (e.g: 2004)
date.getHours() hours-from-dateTime($date) Hour of day (0 - 23)
date.getMinutes() minutes-from-dateTime($date) Minute in hour (0 - 59)
date.getSeconds() seconds-from-dateTime($date) Second in minute (0 - 59)
date.toISOString() string($date) Date in ISO8601 format, e.g: "2013-11-25T15:24:16.370+01:00"

Various

JavaScript XPath
location.href static-base-uri()
new Date().getTimezoneOffset() implicit-timezone()
Have more questions? Submit a request

Comments

Powered by Zendesk