[http://creativecommons.org/licenses/by/3.0/]
This work is licensed under a CC
Attribution 3.0 Unported License [http://creativecommons.org/licenses/by/3.0/]
XML structures data into a rather small number of different constructs, most notably elements and attributes. The XML Path Language (XPath) defines a way how to select parts of XML documents, so that they can be used for further processing. XPath is a very compact language with a syntax that resembles path expressions well-known from file systems. These path expressions, however, are generalized and therefore much more powerful than the rather simple path expressions in file systems. Because of its use in different XML technologies, XPath is one of the most important XML core technologies. With XPath 2.0, the language has been greatly extended, the new version of XPath is the foundation for XSLT 2.0 and XQuery. XPath 2.0 provides support for regular expression matching, typed expressions, and contains language constructs for conditional and repeated evaluation.
ü
/ü
→ ü
)//img[not(@alt)]
→ select all images which have no alt
attributecount(//img)
→ return the number of images/descendant::img[3]/@src
→ return the third image's src
URIstarts-with(/html/@lang, 'en')
→ test whether the document's language is english/
, like path names in file systemsFile System Path: | / |
usr |
/ |
local |
/ |
apache |
/ |
bin |
/ |
---|---|---|---|---|---|---|---|---|---|
# Selected Nodes: | 1 | → 1 | → | 1 | → | 1 | → | 1 |
XPath: | / |
html |
/ |
body |
/ |
table |
/ |
thead |
/ |
tr |
---|---|---|---|---|---|---|---|---|---|---|
# Selected Nodes: | 1 | → 1 | → | 1 | → | 6 | → | 4 | → | 12 |
/html/head/title
/html/head/*
text()
comment()
node()
processing-instruction()
processing-instruction("xml-stylesheet")
.
and ..
are clever directory shortcutsfind
)child
descendant::a
xmlns
) are invisible/
preceding::p[@class="warning"]
)::
(no axis uses the default axis child
)[]
child::
can be omitted (default axis)attribute::
can be written as @
.
is an abbreviation for self::node()
..
is an abbreviation for parent::node()
//
is an abbreviation for /descendant-or-self::node()/
position()
function (/descendant::a[5]
)//img[not(@alt)]
count(//img)
/descendant::img[3]/@src
starts-with(/html/@lang, 'en')
NaN
)//a[starts-with(@href, https)]
)boolean, contains, false, lang, not, starts-with, true
ceiling, count, floor, last, number, position, round, string-length, sum
concat, local-name, name, namespace-uri, normalize-space, string, substring, substring-after, substring-before, translate
id
//a[substring(@href,string-length(@href)-2)='pdf']
<listing src="xlinked-class.xml" line="81-98"/>
string-join(tokenize( if ( exists(@encoding) ) then unparsed-text($fileuri, @encoding) else unparsed-text($fileuri), '\r?\n')[(position() ge number(tokenize(current()/@line, '\-')[1])) and (position() le number(tokenize(current()/@line, '\-')[2]))], '
')
control flowis based on predicates
emulatecontrol flow
if ( … ) then … else …
if ( @sex eq 'm' ) then 'Sir' else 'Madam'
if ( @sex eq 'm' ) then 'Sir' else if ( @sex eq 'f' ) then 'Madam' else 'Whatever'
<names> <name> <first>Erik</first> <last>Wilde</last> </name> <name> <last>Hasan</last> </name> </names>
first | last[not(../first)]
<xsl:variable name="name"> <xsl:choose> <xsl:when test="first"> <xsl:value-of select="first"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="last"/> </xsl:otherwise> </xsl:choose> </xsl:variable>
if ( exists(first) ) then first else last
for $… in … return …
for $i in //name return $i/last
for $i in //name return if ( exists($i/first) ) then $i/first else $i/last
/names/name/last
for $i in /names return for $j in $i/name return $j/last
for $i in 1 to 10 return $i
//last/../..
for $i in //last return for $j in $i/.. return $j/..
( some | every ) $… in … satisfies …
some $i in //*[@xlink:type='locator']/@xlink:href satisfies $i eq $query-uri
every $i in //li/@id satisfies //*[@xlink:type='locator'][@xlink:href=concat('#', $i)]
//img[not(@alt)]
count(//img)
/descendant::img[3]/@src
starts-with(/html/@lang, 'en')
every $i in ( 11, 22, 33, 'string' ) satisfies string(number($i)) ne 'NaN'
regular expressions for XML
for $i in ( 11, 22, 33, 'string' ) return ($i, number($i))
declare variable $firstName external; <videos featuring="{$firstName}"> { let $doc := . for $v in $doc//video, $a in $doc//actors/actor where ends-with($a, $firstName) and $v/actorRef = $a/@id order by $v/year return <video year="{$v/year}"> { $v/title } </video> } </videos>
a query is a transformation is a query