Domestico » Rete del puntino

Lezione privata di XPath con gli esempi

Questo alberino è una piccola introduzione per xpath (lingua del percorso di XML) è una sintassi terse (del non-XML) per le parti di richiamo di un documento di XML. XPath è stato adottato velocemente dagli sviluppatori come piccola lingua di domanda.

Con l'aumento nell'uso di xml che è trasmesso sul Internet per i dati di trasferimento ci era bisogno enorme di un certo senso standard dell'accesso a dei dati in un modo semplice e veloce. Quello è che cosa è Xpath è circa.

Introduzione a Xpath

XPath è una lingua per l'individuazione delle informazioni in un documento di XML. Permette che i consumatori di Xml lo interroghino e gli accedano direttamente qualsiasi parte. Può restituire la posizione di segnale minimo, una stringa, un numero o un nodo in se del xml. XPath è usato per traversare con gli elementi e gli attributi in un documento di XML.

Che cosa è XPath?

  • XPath è una sintassi per la definizione delle parti di un documento di XML.
  • XPath usa le espressioni del percorso per traversare nei documenti di XML.
  • XPath contiene una biblioteca delle funzioni standard.
  • XPath è un elemento importante in XSLT.
  • XPath è un campione di W3C.

Ora in cui dice l'espressione non stiamo comunicando le espressioni normali spaventose del abt ma le espressioni semplici cui la usiamo per il nostro tavolo che cerca spesso come [. *]. Ora lascia l'inizio con un esempio e vanno avanti in questo.

Lima di esempio XML.

<? version= del xml? 1.0? encoding=? ISO-8859-1??>

<bookstore> [nodo del documento]<

lang=>< di titolo del libro? en? >[attributo] Harry Potter< /title> [elemento]<

autore> JKRowling< /author>

<anno> /year< 2005>

<prezzo> 29.99< /price>

</book>

</bookstore>

Un XML è trattare come un albero dei nodi. Se osserviamo molto attentamente ci sono intorno sette che il genere di nodi definiti dalla radice di XPath.The di questo albero è denominato come il nodo del documento o nodo della radice. I nodi sono elemento, attributo, testo, namespace, proced-istruzione, commento e nodi del documento (radice).

NODI DI XML

  1. il bookstore è nodo della radice, il titolo è un elemento e un lang=? en? è l'attributo.
  2. L'elemento e l'attributo di ParentEach ha un genitore. Nell'esempio; l'elemento del libro è il genitore del titolo, autore, anno e prezzo.
  3. Bambini: I nodi dell'elemento possono avere zero, uno o più bambini. Nell'esempio; il titolo, l'autore, l'anno e gli elementi di prezzi sono tutti i bambini dell'elemento del libro.
  4. Fratelli germani: Nodi che hanno lo stesso genitore. Nell'esempio; il titolo, l'autore, l'anno e gli elementi di prezzi sono tutti i fratelli germani.
  5. Antenati: Un nodo? genitore di s, genitore? genitore di s, ecc. Nell'esempio; the ancestors of the title element are the book element and the bookstore element.
  6. Descendants: A node?s children, children?s children, etc. In the example; descendants of the bookstore element are the book, title, author, year, and price elements.

Now since we are clear with the basics of xpath we can take an example of it and work along.Let?s try to learn some basic XPath syntax by looking at some examples.

The XML Example Document

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

Selecting Nodes:


We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:

set xmlDoc=CreateObject(?Microsoft.XMLDOM?)
xmlDoc.async=?false?
xmlDoc.load(?books.xml?)xmlDoc.selectNodes(path expression)

Select all book Nodes:
The following example selects all the book nodes under the bookstore element:

xmlDoc.selectNodes(?/bookstore/book?)

Select the First book Node:

The following example selects only the first book node under the bookstore element:

xmlDoc.selectNodes(?/bookstore/book[0]?)

Select the prices:

The following example selects the text from all the price nodes:

xmlDoc.selectNodes(?/bookstore/book/price/text()?)

Selecting price Nodes with Price>35:

The following example selects all the price nodes with a price higher than 35:

xmlDoc.selectNodes(?/bookstore/book[price>35]/price?)

Thus from the above example we can see how to navigate through an xml document and navigate through it and retrieve the data.This was just a small idea how it works.XPath is bigger than this.If you have still some query i would refer to go through more books to see the actual implementation of it in detail.

For a Complete details i would like to recommend to go through standards and follow the links

External links:

Tags: , ,

Translate to EnglishÜbersetzen Sie zum Deutsch/GermanΜεταφράστε στα ελληνικά/GreekПереведите к русскому/RussianOversetter til Norsk/NorwegianÖversätta till Svensk/Swedishहिनà¥à¤¦à¥€ अनà¥à¤µà¤¾à¤¦ करने के लिà¤/Hindi
Tradueix al català/CatalanTulkot uz latviešu/LatvianPreložiť do slovenčiny/SlovakVertaal aan het Nederlands/Dutchترجمة الى العربية/ArabicTraduzca al Español/SpanishTraduisez au Français/French
Traduca ad Italiano/ItalianTraduza ao Português/Portuguese日本語に翻訳しなさい /Japanese한국어에게 번역하십시오/Korean中文翻译/Chinese Simplified中文翻译/Chinese TraditionalПереклад на українську/Ukrainian
We Recommend : Click here to run a Free Performance scan
SMS subscribe Print This Post

Posted on 1st January 2008 by Ashish Mohta , A tech blogger who writes about solving day to day problems of people who use computer. He also writes on How to use the applications like Office, PC tips, Online tools,Browsers and more. All posts by Ashish Mohta | Connect with me @ Twitter | Linkedin | Facebook | Stumble | Need more help? Ask your Questions at our Support Center



Leave your response!

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>