XPath अक्ष
एक्सएमएल उदाहरण दस्तावेज़
हम नीचे दिए गए उदाहरणों में निम्नलिखित 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>
XPath अक्ष
एक अक्ष संदर्भ (वर्तमान) नोड के संबंध का प्रतिनिधित्व करता है, और पेड़ पर उस नोड के सापेक्ष नोड्स का पता लगाने के लिए उपयोग किया जाता है।
AxisName | Result |
---|---|
ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node |
ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself |
attribute | Selects all attributes of the current node |
child | Selects all children of the current node |
descendant | Selects all descendants (children, grandchildren, etc.) of the current node |
descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself |
following | Selects everything in the document after the closing tag of the current node |
following-sibling | Selects all siblings after the current node |
namespace | Selects all namespace nodes of the current node |
parent | Selects the parent of the current node |
preceding | Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes |
preceding-sibling | Selects all siblings before the current node |
self | Selects the current node |
स्थान पथ अभिव्यक्ति
एक स्थान पथ पूर्ण या सापेक्ष हो सकता है।
एक पूर्ण स्थान पथ एक स्लैश ( / ) से शुरू होता है और एक सापेक्ष स्थान पथ नहीं होता है। दोनों ही मामलों में स्थान पथ में एक या अधिक चरण होते हैं, प्रत्येक को एक स्लैश द्वारा अलग किया जाता है:
An absolute location path:
/step/step/...
A relative location path:
step/step/...
प्रत्येक चरण का मूल्यांकन वर्तमान नोड-सेट में नोड्स के विरुद्ध किया जाता है।
एक कदम से मिलकर बनता है:
- एक अक्ष (चयनित नोड्स और वर्तमान नोड के बीच ट्री-रिलेशनशिप को परिभाषित करता है)
- एक नोड-परीक्षण (एक अक्ष के भीतर एक नोड की पहचान करता है)
- शून्य या अधिक विधेय (चयनित नोड-सेट को और अधिक परिष्कृत करने के लिए)
स्थान चरण के लिए सिंटैक्स है:
axisname::nodetest[predicate]
उदाहरण
Example | Result |
---|---|
child::book | Selects all book nodes that are children of the current node |
attribute::lang | Selects the lang attribute of the current node |
child::* | Selects all element children of the current node |
attribute::* | Selects all attributes of the current node |
child::text() | Selects all text node children of the current node |
child::node() | Selects all children of the current node |
descendant::book | Selects all book descendants of the current node |
ancestor::book | Selects all book ancestors of the current node |
ancestor-or-self::book | Selects all book ancestors of the current node - and the current as well if it is a book node |
child::*/child::price | Selects all price grandchildren of the current node |