XML და XPath

XML და XPath

XSLT-ით თქვენ შეგიძლიათ XML დოკუმენტის ტრანსფორმაცია HTML-ში.


რა არის XPath?

XPath (XML Path Language) არის ენა, რომელიც გამოიყენება XML დოკუმენტების ნაწილების მისათითებლად (point) ან მოსანიშნად (select).

XPath არ არის სრულყოფილი ენა, არამედ სინტაქსი სხვა ენების მიერ გამოსაყენებლად.

XPath განვითარდა W3C-ის მიერ, როგორც ძირითადი ელემენტი W3C სტანდარტების, XSLT, XQuery, XLink, და XPointer.

დღესდღეობით XPath შეიძლება ასევე გამოყენებული იყოს JavaScript-სა და Java-ში, XML სქემაში, PHP-ში, Python-ში, C-სა და C++-ში, და სხვა მრავალ ენაში.


XML ნიმუში

ჩვენ გამოვიყენებთ ქვემოთმოცემულ XML დოკუმენტს სინტაქსის ნიმუშებისთვის:

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

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


XPath სინტაქსტი

ქვემოთ მოცემულ ცხრილში ჩამოთვლილია XPath-ის გამოხატულებები (expressions) და მათი შედეგები:

Path გამოხატულებაშედეგი
/bookstore/book[1] მონიშნავს პირველ ელემენტს book-ს, რომელიც არის bookstore ელემენტის შვილი ელემენტი.

შენიშვნა: IE5 და მოგიანო ვერსიებმა დანეგრეს, რომ [0] უნდა იყოს პირველი კვანძი. თუმცა W3C სტანდარტის მიხედვით, ის უნდა ყოფილიყო [1]!!

/bookstore/book[last()] მონიშნავს ბოლო book ელემენტს, რომელიც არის bookstore ელემენტის შვილი ელემენტი.
/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='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng'
/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

თუ გსურთ,შეიტყოთ მეტი XPath-ის შესახებ, მოიძიეთ XPath-ის ტუტორიალი ჩვენს ვებგვერდზე.


Last modified: პარასკევი, თებერვალი 28 2014, 1:47