Samouczek XML

Strona główna XML Wprowadzenie do XML XML Jak używać Drzewo XML Składnia XML Elementy XML Atrybuty XML Przestrzenie nazw XML Wyświetlacz XML Żądanie HTTP XML Parser XML XML DOM XML XPath XML XSLT XML XQuery XML XLink Walidator XML XML DTD Schemat XML Serwer XML Przykłady XML Quiz XML Certyfikat XML

XML AJAX

Wprowadzenie do AJAX AJAX XMLHttp Żądanie AJAX Odpowiedź AJAX Plik XML AJAX AJAX PHP ASP AJAX Baza danych AJAX Aplikacje AJAX Przykłady AJAX

XML DOM

Wprowadzenie do DOM Węzły DOM Dostęp do DOM Informacje o węźle DOM Lista węzłów DOM Przechodzenie DOM Nawigacja DOM DOM Uzyskaj wartości Zmiana węzłów DOM DOM Usuń węzły DOM Zamień węzły DOM Utwórz węzły DOM Dodaj węzły Węzły klonowania DOM Przykłady DOM

Samouczek XPath

Wprowadzenie do XPath Węzły XPath Składnia XPath Osie XPath Operatorzy XPath Przykłady XPath

Samouczek XSLT

Wprowadzenie XSLT Języki XSL Transformacja XSLT XSLT <szablon> XSLT <wartość-z> XSLT <dla każdego> XSLT <sortuj> XSLT <jeśli> XSLT <wybierz> XSLT Zastosuj XSLT na kliencie XSLT na serwerze XSLT Edytuj XML Przykłady XSLT

Samouczek XQuery

Wprowadzenie do XQuery Przykład XQuery XQuery FLWOR XQuery HTML Warunki XQuery Składnia XQuery XQuery Dodaj XQuery Wybierz Funkcje XQuery

XML DTD

Wprowadzenie do DTD Bloki konstrukcyjne DTD Elementy DTD Atrybuty DTD Elementy DTD a Attr Jednostki DTD Przykłady DTD

Schemat XSD

Wprowadzenie do XSD XSD Jak to zrobić XSD <schemat> Elementy XSD Atrybuty XSD Ograniczenia XSD

Kompleks XSD

Elementy XSD XSD Pusty Tylko elementy XSD Tylko tekst XSD Mieszane XSD Wskaźniki XSD XSD <dowolny> XSD <dowolnyAtrybut> Zastąpienie XSD Przykład XSD

Dane XSD

XSD ciąg Data XSD Numeryczne XSD XSD Różne Odniesienie XSD

Usługi internetowe

Usługi XML XML WSDL MYDŁO XML XML RDF XML RSS

Bibliografia

Typy węzłów DOM Węzeł DOM Lista węzłów DOM DOM NamedNodeMap Dokument DOM Element DOM Atrybut DOM Tekst DOM DOM CDATA Komentarz DOM DOM XMLHttpRequest Analizator DOM Elementy XSLT Funkcje XSLT/XPath

Składnia XPath


XPath używa wyrażeń ścieżki do wybierania węzłów lub zestawów węzłów w dokumencie XML. Węzeł jest wybierany podążając ścieżką lub krokami.


Przykładowy dokument XML

W poniższych przykładach użyjemy następującego dokumentu XML.

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book>
  <title lang="en">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="en">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

Wybieranie węzłów

XPath używa wyrażeń ścieżki do wybierania węzłów w dokumencie XML. Węzeł jest wybierany podążając ścieżką lub krokami. Poniżej wymieniono najbardziej przydatne wyrażenia ścieżki:

Expression Description
nodename Selects all nodes with the name "nodename"
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

W poniższej tabeli wymieniliśmy niektóre wyrażenia ścieżki i wynik wyrażeń:

Path Expression Result
bookstore Selects all nodes with the name "bookstore"
/bookstore Selects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

bookstore/book Selects all book elements that are children of bookstore
//book Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element
//@lang Selects all attributes that are named lang


Predykaty

Predykaty służą do znajdowania określonego węzła lub węzła zawierającego określoną wartość.

Predykaty są zawsze osadzone w nawiasach kwadratowych.

W poniższej tabeli wymieniliśmy kilka wyrażeń ścieżki z predykatami i wynikiem wyrażeń:

Path Expression Result
/bookstore/book[1] Selects the first book element that is the child of the bookstore element.

Note: In IE 5,6,7,8,9 first node is[0], but according to W3C, it is [1]. To solve this problem in IE, set the SelectionLanguage to XPath:

In JavaScript: xml.setProperty("SelectionLanguage","XPath");
/bookstore/book[last()] Selects the last book element that is the child of the bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element
//title[@lang] Selects all the title elements that have an attribute named lang
//title[@lang='en'] Selects all the title elements that have a "lang" attribute with a value of "en"
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

Wybieranie nieznanych węzłów

Symbole wieloznaczne XPath mogą służyć do wybierania nieznanych węzłów XML.

Wildcard Description
* Matches any element node
@* Matches any attribute node
node() Matches any node of any kind

W poniższej tabeli wymieniliśmy niektóre wyrażenia ścieżki i wynik wyrażeń:

Path Expression Result
/bookstore/* Selects all the child element nodes of the bookstore element
//* Selects all elements in the document
//title[@*] Selects all title elements which have at least one attribute of any kind

Wybór kilku ścieżek

Używając | operatora w wyrażeniu XPath można wybrać kilka ścieżek.

W poniższej tabeli wymieniliśmy niektóre wyrażenia ścieżki i wynik wyrażeń:

Path Expression Result
//book/title | //book/price Selects all the title AND price elements of all book elements
//title | //price Selects all the title AND price elements in the document
/bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document