Nodi XPath

In XPath ci sono sette tipi di nodi: elemento, attributo, testo, spazio nome, istruzione di processamento, commento e nodo documento (o nodo radice).

Terminologia XPath

Nodo

In XPath ci sono sette tipi di nodi: elemento, attributo, testo, spazio nome, istruzione di processamento, commento e nodo documento (o nodo radice). Il documento XML viene trattato come un albero di nodi. La radice dell'albero è chiamata nodo documento o nodo radice.

Vediamo questo documento XML di seguito:

<?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>

Esempi di nodi nel documento XML sopra:

<bookstore> (nodo documento)
<author>J K. Rowling</author> (nodo elemento)
lang="en" (nodo attributo) 

Valore base (o valore atomo)

Un valore base è un nodo senza padre o figli.

Esempi di valori base:

J K. Rowling
"en"

Progetto

Un progetto è un valore base o un nodo.

Relazioni tra nodi

Padre

Ogni elemento e attributo ha un padre.

Nell'esempio seguente, l'elemento book è il padre degli elementi title, author, year e price:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Figlio

Un nodo elemento può avere zero, uno o più figli.

Nell'esempio seguente, gli elementi title, author, year e price sono figli dell'elemento book:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Gemello

Nodi che condividono lo stesso padre

Nell'esempio seguente, gli elementi title, author, year e price sono gemelli:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

Genitore

Il padre di un nodo, il padre del padre, ecc.

Nell'esempio seguente, i genitori di l'elemento title sono l'elemento book e l'elemento bookstore:

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>

Discendente

Il figlio di un nodo, il figlio del figlio, ecc.

Nell'esempio seguente, i discendenti di bookstore sono gli elementi book, title, author, year e price:

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>