1 xpath. 2 what is xpath? xpath is a syntax for defining parts of an xml document xpath uses paths...

23

Click here to load reader

Upload: santo-campo

Post on 02-May-2015

255 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

1

XPath

Page 2: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

2

What is XPath?

● XPath is a syntax for defining parts of an XML document

● XPath uses paths to define XML elements ● XPath defines a library of standard functions ● XPath is a major element in XSLT ● XPath is not written in XML ● XPath is a W3C Standard

Page 3: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

3

Nodi

● In XPath un documento XML viene considerato una struttura ad albero composta da nodi

● Xpath ha 7 tipi di nodi:– Radice– Elemento– Attributo– Testo– Commento– Istruzione di elaborazione– Namespace

Page 4: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

4

Nodi● La struttura ad albero di Xpath ha un solo nodo radice● Il nodo radice ed i nodi degli elementi contengono liste

ordinate di nodi child (figli)● Tutti i nodi tranne quello radice hanno un nodo parent

(genitore)● Possono essere nodi child solamente:

– Elementi, commenti, testi, istruzioni di elaborazione

● Notare che anche se un nodo attributo o namespace può avere come parent un elemento o la radice, non viene considerato un figlio del suo parent

Page 5: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

5

Tipi di Nodi XPath

Page 6: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

6

Tipi di nodi XPath

Page 7: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

7

Esempio di albero XPath<?xml version="1.0"?>

<!-- Esempio di file XML -->

<!-- per lo studio di XPath -->

<book title="C++ How to Program" edition="3">

<sample>

<![CDATA[

// C++ comment

if ( this->getX() < 5 && value[ 0 ] != 3 )

cerr << this->displayError();

]]>

</sample>

C++ How to Program by Deitel &amp; Deitel

</book>

Page 8: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

8

L'albero relativo al file XML precedenteRadice

CommentoEsempio di file XML

CommentoPer lo studio di XPath

Elementobook

AttributoTitle

C++ How to program

AttributoEdition

3

ElementoSample

Testo// C++ comment

if ( this->getX() < 5 && value[ 0 ] != 3 )cerr << this->displayError();

TestoC++ How to Program by Deitel &amp; Deitel

Radice

CommentoEsempio di file XML

CommentoPer lo studio di XPath

Elementobook

AttributoTitle

C++ How to program

AttributoEdition

3

ElementoSample

Testo// C++ comment

if ( this->getX() < 5 && value[ 0 ] != 3 )cerr << this->displayError();

TestoC++ How to Program by Deitel &amp; Deitel

Page 9: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

9

I path di posizione

● Un path di posizione e' un'espressione che specifica come spostarsi fra i nodi di un albero Xpath

● Un path di posizione è composto da:– Un asse– Un test dei nodi– Un predicato (facoltativo)

Page 10: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

10

Gli assi● Le ricerche iniziano sempre dal nodo di contesto

– Selezionabile ad esempio con una istruzione match nelle trasformazioni XSL che vedremo poi

● Un asse indica quali nodi riferiti al nodo di contesto deveono essere inclusi nella ricerca

● L'asse determina la sequenza di ricerca– Diretta (vengono selezionati i nodi seguenti (child) di quello

di contesto)– Inversa (vengono selezionati i nodi antecedenti (parent) a

quello di contesto ● Tipo di nodo principale dell'asse:

– Il tipo di nodo che l'asse puo' selezionare

Page 11: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

11

Gli assi XPath

Page 12: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

12

Test dei nodi

● Un asse seleziona un gruppo di nodi dall'albero in base ad un test dei nodi

Page 13: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

13

Path di posizione = Assi + Test dei nodi● I path di posizione sono formati da sequenze di passaggi di

posizione separati da /● Un passaggio di posizione è formato da:

– Un asse– ::– Un test dei nodi– Un predicato (tra parentesi quadre, facoltativo)

● Esempi:– child::* (seleziona tutti i nodi degli elementi child del nodo

di contesto)– child::text() (seleziona tutti i nodi di testo child del nodo di

contesto)– child::body (seleziona tutti i nodi dell'elemento body che

sono child del nodo di contesto)

Page 14: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

14

Esempio● Dal file XML seguente si vogliono selezionare tutti i libri

tradotti in giapponese, in una o più edizioni<?xml version="1.0"?><!-- Fig. 11.9 : books.xml --><!-- XML book list --><books>

<book><title>Java How to Program</title><translation edition="1">Spanish</translation><translation edition="1">Chinese</translation><translation edition="1">Japanese</translation><translation edition="2">French</translation><translation edition="2">Japanese</translation>

</book><book>

<title>C++ How to Program</title><translation edition="1">Korean</translation><translation edition="2">French</translation><translation edition="2">Spanish</translation><translation edition="3">Italian</translation><translation edition="3">Japanese</translation>

</book></books>

Page 15: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

15

Esempio

● Si supponga che il nodo radice (books) sia il nodo di contesto

● Il path di posizione da utilizzare per selezionare l'elemento title di qualunque libro tradotto in giapponese è:/books/book/translation[. = 'japanese']/../title

● Il path di posizione da utilizzare per selezionare l'attributo edition di qualunque libro tradotto in giapponese è:/books/book/translation[. = 'japanese']/@edition

Page 16: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

16

Operatori e funzioni

● Gli operatori XPath consentono di manipolare i gruppi di nodi selezionati con i path di posizione per formare altri gruppi

● Le funzioni XPath permettono di svolgere operazioni su un gruppo di nodi

Page 17: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

17

File XML con Namespace

<?xml version="1.0"?><!-- Fig. 11.3 : simple2.xml --><!-- Processing instructions and namespacess --><html xmlns="http://www.w3.org/TR/REC-html40">

<head><title>Processing Instruction and Namespace Nodes</title>

</head><?deitelprocessor example = "fig11_03.xml"?><body>

<deitel:book deitel:edition="1" xmlns:deitel="http://www.deitel.com/xmlhtp1">

<deitel:title>XML How to Program</deitel:title></deitel:book>

</body></html>

Page 18: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

18

Lista operatori XPath● Pipe (|)

– Unisce due gruppi di nodi– head | body (seleziona tutti gli elementi head e body

che sono child del nodo di contesto)● Slash(/)

– Separa i passaggi di posizione– head/title[ last () ] (seleziona l'ultimo nodo

dell'elemento title che è contenuto in head)● Doppio Slash (//)

– Equivalente a /descendant-or-self::node()/– //body è equivalente a

/descendant-or-self::node()/child()::body(seleziona tutti i nodi body dell'intero documento)

Page 19: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

19

Lista funzioni XPath

Page 20: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

20

Esempi di funzioni

● book [ position()=3 ] (oppure book[ 3 ])

Seleziona il terzo elemento book del nodo di contesto● count (*)

restituisce il numero totale di elementi che sono child del nodo di contesto

● //book

seleziona tutti i nodi dell'elemento book

Page 21: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

21

Esempio sigle società di borsa

<?xml version="1.0"?><!-- Fig. 11.13 : stocks.xml --><!-- Stock list --><stocks>

<stock symbol="INTC"><name>Intel Corporation</name>

</stock><stock symbol="CSCO">

<name>Cisco Systems, Inc.</name></stock><stock symbol="DELL">

<name>Dell Computer Corporation</name></stock><stock symbol="MSFT">

<name>Microsoft Corporation</name></stock><stock symbol="SUNW">

<name>Sun Microsystems, Inc.</name></stock><stock symbol="CMGI">

<name>CMGI, Inc.</name></stock>

</stocks>

Vogliamo selezionare tutte le società la cui sigla

inizia per C

Page 22: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

22

Foglio di stile per società di borsa<?xml version = "1.0"?><!-- Fig. 11.14 : stocks.xsl --><!-- string function usage --><xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> <xsl:template match = "/stocks"> <html> <body> <ul> <xsl:for-each select = "stock"> <xsl:if test = "starts-with(@symbol, 'C')"> <li> <xsl:value-of select = "concat(@symbol,' - ', name)"/> </li> </xsl:if> </xsl:for-each> </ul> </body> </html> </xsl:template></xsl:stylesheet>

Selezione del nodo di contesto

Funzione starts-with

Concatenazione della sigla con il nome

Page 23: 1 XPath. 2 What is XPath? XPath is a syntax for defining parts of an XML document XPath uses paths to define XML elements XPath defines a library of standard

23

Risultato