Archite (Ure E Proge (O Dei Sistemi Web: Xpath
Archite (Ure E Proge (O Dei Sistemi Web: Xpath
SlsLeml Web
xaLh
Sommarlo
lnLroduzlone
nodl
SlnLassl
Assl
CperaLorl
xaLh ln !avaScrlpL
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 2
lnLroduzlone
xaLh ls a language for ndlng lnformauon ln an xML
documenL.
xaLh ls a synLax for denlng parLs of an xML documenL
xaLh uses paLh expresslons Lo navlgaLe ln xML
documenLs
xaLh conLalns a llbrary of sLandard funcuons
xaLh ls a ma[or elemenL ln xSL1
xaLh ls a W3C recommendauon
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 3
lnLroduzlone
xaLh aLh Lxpresslons
xaLh uses paLh expresslons Lo selecL nodes or node-seLs ln an xML
documenL. 1hese paLh expresslons look very much llke Lhe expresslons you
see when you work wlLh a Lradluonal compuLer le sysLem.
xaLh SLandard luncuons
xaLh lncludes over 100 bullL-ln funcuons. 1here are funcuons for sLrlng
values, numerlc values, daLe and ume comparlson, node and Cname
manlpulauon, sequence manlpulauon, 8oolean values, and more.
xaLh ls used ln xSL1
xaLh ls a ma[or elemenL ln Lhe xSL1 sLandard. WlLhouL xaLh knowledge you
wlll noL be able Lo creaLe xSL1 documenLs.
xaLh became a W3C 8ecommendauon 16. november 1999.
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 4
nodl
ln xaLh, Lhere are seven klnds of !"#$%:
elemenL, aurlbuLe, LexL, namespace,
processlng-lnsLrucuon, commenL, and
documenL nodes.
xML documenLs are LreaLed as Lrees of nodes.
1he LopmosL elemenL of Lhe Lree ls called Lhe
rooL elemenL.
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 5
Lsemplo
xML documenL
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>#
nodes
<bookstore> (root element node)
<author>J K. Rowling</author> (element node)
lang="en" (attribute node)#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 6
valorl aLomlcl
&'"()* ,-./$% are nodes wlLh no chlldren or
parenL.
Lxample of aLomlc values:
! k. 8owllng
"en"
0'$(% are aLomlc values or nodes.
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 7
8elazlonl fra nodl
1-2$!'
Lach elemenL and aurlbuLe has one parenL.
ln Lhe followlng example, Lhe book elemenL ls Lhe parenL of
Lhe uLle, auLhor, year, and prlce:
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 8
8elazlonl fra nodl
34).#2$!
LlemenL nodes may have zero, one or more chlldren.
ln Lhe followlng example, Lhe uLle, auLhor, year, and prlce
elemenLs are all chlldren of Lhe book elemenL:
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 9
8elazlonl fra nodl
5)6.)!7%
nodes LhaL have Lhe same parenL.
ln Lhe followlng example, Lhe uLle, auLhor, year, and prlce
elemenLs are all slbllngs:
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 10
8elazlonl fra nodl
&!*$%'"2%
A node's parenL, parenL's parenL, eLc.
ln Lhe followlng example, Lhe ancesLors of Lhe uLle elemenL
are Lhe book elemenL and Lhe booksLore elemenL:
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 11
8elazlonl fra nodl
8$%*$!#-!'%
A node's chlldren, chlldren's chlldren, eLc.
ln Lhe followlng example, descendanLs of Lhe booksLore
elemenL are Lhe book, uLle, auLhor, year, and prlce
elemenLs:
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 12
SlnLassl
xaLh uses paLh expresslons Lo selecL nodes or
node-seLs ln an xML documenL.
1he node ls selecLed by followlng a paLh or
sLeps.
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 13
Lspresslonl
9:;2$%%)"! 8$%*2);<"!
!"#$!%&$ SelecLs Lhe named node
/ SelecLs from Lhe rooL node
// SelecLs nodes ln Lhe documenL from Lhe currenL node LhaL
maLch Lhe selecuon no mauer where Lhey are
. SelecLs Lhe currenL node
.. SelecLs Lhe parenL of Lhe currenL node
[ SelecLs aurlbuLes
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 14
Lsemplo
#<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
#<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
#</bookstore>#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 15
Lsempl dl espresslonl
1-'4 9:;2$%%)"! =$%/.'
booksLore SelecLs Lhe booksLore elemenL
/booksLore SelecLs Lhe rooL elemenL booksLore
>"'$? lf Lhe paLh sLarLs wlLh a slash ( / ) lL always
represenLs an absoluLe paLh Lo an elemenL!
booksLore/book SelecLs all book elemenLs LhaL are chlldren of booksLore
//book SelecLs all book elemenLs no mauer where Lhey are ln Lhe
documenL
booksLore//book SelecLs all book elemenLs LhaL are descendanL of Lhe
booksLore elemenL, no mauer where Lhey are under Lhe
booksLore elemenL
//[lang SelecLs all aurlbuLes LhaL are named lang
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 16
redlcau
redlcaLes are used Lo nd a speclc node or a
node LhaL conLalns a speclc value.
redlcaLes are always embedded ln square
brackeLs.
ln Lhe nexL sllde are llsLed some paLh
expresslons wlLh predlcaLes and Lhe resulL of
Lhe expresslons
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 17
1-'4 9:;2$%%)"! =$%/.'
/booksLore/book[1] SelecLs Lhe rsL book elemenL LhaL ls Lhe chlld of Lhe booksLore
elemenL.
/booksLore/book[lasL()] SelecLs Lhe lasL book elemenL LhaL ls Lhe chlld of Lhe booksLore
elemenL
/booksLore/book[lasL()-1] SelecLs Lhe lasL buL one book elemenL LhaL ls Lhe chlld of Lhe
booksLore elemenL
/booksLore/book[posluon()<3] SelecLs Lhe rsL Lwo book elemenLs LhaL are chlldren of Lhe
booksLore elemenL
//uLle[[lang] SelecLs all Lhe uLle elemenLs LhaL have an aurlbuLe named lang
//uLle[[lang='eng'] SelecLs all Lhe uLle elemenLs LhaL have an aurlbuLe named lang
wlLh a value of 'eng'
/booksLore/book[prlce>33.00] SelecLs all Lhe book elemenLs of Lhe booksLore elemenL LhaL
have a prlce elemenL wlLh a value greaLer Lhan 33.00
/booksLore/book[prlce>33.00]/
uLle
SelecLs all Lhe uLle elemenLs of Lhe book elemenLs of Lhe
booksLore elemenL LhaL have a prlce elemenL wlLh a value
greaLer Lhan 33.00
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 18
8lcerca dl nodl
xaLh wlldcards can be used Lo selecL unknown xML
elemenLs.
Lsemplo
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 19
@).#*-2# 8$%*2);<"!
* MaLches any elemenL node
[* MaLches any aurlbuLe node
1-'4 9:;2$%%)"! =$%/.'
/booksLore/* SelecLs all Lhe chlld nodes of Lhe booksLore elemenL
//* SelecLs all elemenLs ln Lhe documenL
//uLle[[*] SelecLs all uLle elemenLs whlch have any aurlbuLe
Selezlone dl paLh mulupll
8y uslng Lhe | operaLor ln an xaLh expresslon you can selecL
several paLhs.
ln Lhe Lable below are llsLed some paLh expresslons and Lhe
resulL of Lhe expresslons:
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 20
1-'4 9:;2$%%)"! =$%/.'
//book/uLle | //book/prlce SelecLs all Lhe uLle Anu prlce elemenLs of all
book elemenLs
//uLle | //prlce SelecLs all Lhe uLle Anu prlce elemenLs ln Lhe
documenL
/booksLore/book/uLle | //prlce SelecLs all Lhe uLle elemenLs of Lhe book
elemenL of Lhe booksLore elemenL Anu all Lhe
prlce elemenLs ln Lhe documenL
Assl
An axls denes a node-seL relauve Lo Lhe currenL
node.
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 21
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 22
&:)%>-($ =$%/.'
ancesLor SelecLs all ancesLors (parenL, grandparenL, eLc.) of Lhe currenL node
ancesLor-or-self SelecLs all ancesLors (parenL, grandparenL, eLc.) of Lhe currenL node and Lhe
currenL node lLself
aurlbuLe SelecLs all aurlbuLes of Lhe currenL node
chlld SelecLs all chlldren of Lhe currenL node
descendanL SelecLs all descendanLs (chlldren, grandchlldren, eLc.) of Lhe currenL node
descendanL-or-self SelecLs all descendanLs (chlldren, grandchlldren, eLc.) of Lhe currenL node and Lhe
currenL node lLself
followlng SelecLs everyLhlng ln Lhe documenL aer Lhe closlng Lag of Lhe currenL node
followlng-slbllng SelecLs all slbllngs aer Lhe currenL node
namespace SelecLs all namespace nodes of Lhe currenL node
parenL SelecLs Lhe parenL of Lhe currenL node
precedlng SelecLs everyLhlng ln Lhe documenL LhaL ls before Lhe sLarL Lag of Lhe currenL node
precedlng-slbllng SelecLs all slbllngs before Lhe currenL node
self SelecLs Lhe currenL node
Locauon aLh Lxpresslon
A locauon paLh can be absoluLe or relauve.
An absoluLe locauon paLh sLarLs wlLh a slash ( / ) and
a relauve locauon paLh does noL. ln boLh cases Lhe
locauon paLh conslsLs of one or more sLeps, each
separaLed by a slash:
An absoluLe locauon paLh:
/step/step/...
A relauve locauon paLh:
step/step/... #
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 23
Locauon aLh Lxpresslon
Lach sLep ls evaluaLed agalnsL Lhe nodes ln Lhe
currenL node-seL.
A sLep conslsLs of:
an axls (denes Lhe Lree-relauonshlp beLween Lhe
selecLed nodes and Lhe currenL node)
a node-LesL (ldenues a node wlLhln an axls)
zero or more predlcaLes (Lo furLher rene Lhe selecLed
node-seL)
1he synLax for a locauon sLep ls:
axlsname::nodeLesL[predlcaLe]
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 24
Lsempl
9:-(;.$ =$%/.'
chlld::book SelecLs all book nodes LhaL are chlldren of Lhe currenL node
aurlbuLe::lang SelecLs Lhe lang aurlbuLe of Lhe currenL node
chlld::* SelecLs all chlldren of Lhe currenL node
aurlbuLe::* SelecLs all aurlbuLes of Lhe currenL node
chlld::LexL() SelecLs all LexL chlld nodes of Lhe currenL node
chlld::node() SelecLs all chlld nodes of Lhe currenL node
descendanL::book SelecLs all book descendanLs of Lhe currenL node
ancesLor::book SelecLs all book ancesLors of Lhe currenL node
ancesLor-or-
self::book
SelecLs all book ancesLors of Lhe currenL node - and Lhe currenL as
well lf lL ls a book node
chlld::*/chlld::prlce SelecLs all prlce grandchlldren of Lhe currenL node
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 25
CperaLorl
An xaLh expresslon reLurns elLher a node-seL,
a sLrlng, a 8oolean, or a number.
nexL sllde ls a llsL of Lhe operaLors LhaL can be
used ln xaLh expresslons
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 26
A;$2-'"2 8$%*2);<"! 9:-(;.$ =$'/2! ,-./$
| CompuLes Lwo node-
seLs
//book | //cd 8eLurns a node-seL wlLh all book and
cd elemenLs
+ Addluon 6 + 4 10
- SubLracuon 6 - 4 2
* Mulupllcauon 6 * 4 24
dlv ulvlslon 8 dlv 4 2
mod Modulus (dlvlslon
remalnder)
3 mod 2 1
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 27
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 28
A;$2-'"2 8$%*2);<"! 9:-(;.$ =$'/2! ,-./$
= Lqual prlce=9.80 Lrue lf prlce ls 9.80
false lf prlce ls 9.90
!= noL equal prlce!=9.80 Lrue lf prlce ls 9.90
false lf prlce ls 9.80
< Less Lhan prlce<9.80 Lrue lf prlce ls 9.00
false lf prlce ls 9.80
<= Less Lhan or equal Lo prlce<=9.80 Lrue lf prlce ls 9.00
false lf prlce ls 9.90
> CreaLer Lhan prlce>9.80 Lrue lf prlce ls 9.90
false lf prlce ls 9.80
>= CreaLer Lhan or
equal Lo
prlce>=9.80 Lrue lf prlce ls 9.90
false lf prlce ls 9.70
or or prlce=9.80 or
prlce=9.70
Lrue lf prlce ls 9.80
false lf prlce ls 9.30
and and prlce>9.00 and
prlce<9.90
Lrue lf prlce ls 9.80
false lf prlce ls 8.30
Lsemplo: selezlona Lum l <uLle>
xhttp = new XMLHttpRequest();#
xhttp.open("GET", "books.xml", false);#
xhttp.send("");#
xml = xhttp.responseXML;#
path = "/bookstore/book/title"
nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);#
result = nodes.iterateNext();#
while (result) {#
document.write(result.childNodes[0].nodeValue);#
document.write("<br />");#
result = nodes.iterateNext();#
}#
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 29
AlLrl esempl
Selezlona ll nodo uLle del prlmo llbro:
/bookstore/book[1]/title#
Selezlona l nodl prezzo con prlce>33:
/bookstore/book[price>35]/price#
Selezlona ll uLle del llbrl con prlce>33:
/bookstore/book[price>35]/title #
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 30
8lepllogo
lnLroduzlone
nodl
SlnLassl
Assl
CperaLorl
xaLh ln !avaScrlpL
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 31
8lsorse ln reLe
hup://www.w3.org/18/xpaLh20/
hup://www.w3schools.com/xpaLh/
hup://xml.hLml.lL/gulde/leggl/60/gulda-xslL/
A.A. 2010-2011 Architetture e Progetto dei Sistemi Web 32