[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/]
The XML Path Language (XPath) is one of the most useful and frequently used languages in the are of XML technologies. In its version 1.0, it is used in technologies such as XSLT, XSD, DOM, and XML Tools. 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<body> <div class="header"> <h1><a href="http://dret.net/lectures/publishing-spring07/">Web-Based Publishing</a> – Class List</h1> <h2><a href="http://www.berkeley.edu/" title="UC Berkeley">UCB</a> <a href="http://ischool.berkeley.edu/" title="School of Information">iSchool</a> – Spring 2007</h2> </div> <ul> <li id="jeff">Jeff Decker</li> <li id="michael">Michael Lee</li> <li id="yiming">Yiming Liu</li> <li id="matty">Matthew Ochmanek</li> <li id="igor">Igor Pesenson</li> <li id="ryan">Ryan Shaw</li> <li id="libby">Libby Smith</li> <li id="john">John Ward</li> <li id="lois">Lois Wei</li> <li id="dret">Erik Wilde</li> </ul> </body>
//ul/li
id('dret')
substring-before(id('dret'), ' ')
count(//ul/li[starts-with(substring-after(., ' '), 'W')])
<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