XPath (XML Path Language) هي لغة تعبيرات لاختيار العقد (Nodes) من مستند XML أو HTML.
الاختبار
منصة اختبار XPath
اختبر وجرب تعبيرات XPath بشكل تفاعلي:
- XPath Testbed (whitebeam.org)
وحدة تحكم المتصفح
$x("//div")
متاح مباشرة داخل أدوات المطورين في Chrome و Edge و Firefox.
المحددات
محددات الأحفاد
| CSS | XPath | المرجع |
|---|---|---|
h1 |
//h1 |
? |
div p |
//div//p |
? |
ul > li |
//ul/li |
? |
ul > li > a |
//ul/li/a |
|
div > * |
//div/* |
|
:root |
/ |
? |
:root > body |
/body |
محددات السمات
| CSS | XPath | المرجع |
|---|---|---|
#id |
//*[@id="id"] |
? |
.class |
//*[@class="class"] |
? |
input[type="submit"] |
//input[@type="submit"] |
|
a#abc[for="xyz"] |
//a[@id="abc"][@for="xyz"] |
? |
a[rel] |
//a[@rel] |
|
a[href^='/'] |
//a[starts-with(@href, '/')] |
? |
a[href$='pdf'] |
//a[ends-with(@href, '.pdf')] |
|
a[href*='://'] |
//a[contains(@href, '://')] |
|
a[rel~='help'] |
//a[contains(@rel, 'help')] |
? |
محددات الترتيب والفهرس
| CSS | XPath | المرجع |
|---|---|---|
ul > li:first-of-type |
//ul/li[1] |
? |
ul > li:nth-of-type(2) |
//ul/li[2] |
|
ul > li:last-of-type |
//ul/li[last()] |
|
li#id:first-of-type |
//li[1][@id="id"] |
? |
a:first-child |
//*[1][name()="a"] |
|
a:last-child |
//*[last()][name()="a"] |
العناصر الشقيقة
| CSS | XPath | المرجع |
|---|---|---|
h1 ~ ul |
//h1/following-sibling::ul |
? |
h1 + ul |
//h1/following-sibling::ul[1] |
|
h1 ~ #id |
//h1/following-sibling::*[@id="id"] |
المكافئات في jQuery
| jQuery | XPath | المرجع |
|---|---|---|
$('ul > li').parent() |
//ul/li/.. |
? |
$('li').closest('section') |
//li/ancestor-or-self::section |
|
$('a').attr('href') |
//a/@href |
? |
$('span').text() |
//span/text() |
الشروط والمنطق
| الشرط | XPath | المرجع |
|---|---|---|
h1:not([id]) |
//h1[not(@id)] |
? |
| مطابقة تامة للنص | //button[text()="Submit"] |
? |
| مطابقة نص جزئي | //button[contains(text(),"Go")] |
|
| شرط حسابي | //product[@price > 2.50] |
|
| يمتلك أي عنصر ابن | //ul[*] |
|
| يمتلك عنصراً ابناً محدداً | //ul[li] |
|
| منطق 'OR' | //a[@name or @href] |
? |
| اتحاد (دمج مجموعات) | //a | //div |
? |
فحص الفئة (Class)
//div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]
تفتقر XPath 1.0 إلى معامل لمطابقة الكلمات المفصولة بمسافات؛ يطابق هذا الحل القياسي أسماء الفئات الكاملة بأمان.
التعبيرات
الخطوات والمحاور
تشريح التعبير:
// |
ul |
/ |
a[@id='link'] |
|---|---|---|---|
| المحور | الخطوة | المحور | الخطوة |
البادئات
| البادئة | مثال | المعنى |
|---|---|---|
// |
//hr[@class='edge'] |
في أي مكان بالمستند |
./ |
./a |
نسبةً إلى السياق الحالي |
/ |
/html/body/div |
من العنصر الجذر |
يمكن أن تبدأ التعبيرات بأي من هذه البادئات.
المحاور
| المحور | مثال | المعنى |
|---|---|---|
/ |
//ul/li/a |
عقدة ابن مباشر |
// |
//*[@id="list"]//a |
أي عقدة حفيدة |
تُفصل الخطوات بعلامة /. استخدم // لمطابقة أي مستوى من الأحفاد.
الخطوات
//div
//div[@name='box']
//*[@id='link']
يمكن أن تحتوي الخطوات على أسماء وسوم والمسندات ([...])، وكلاهما اختياري. كما يمكن للخطوات استهداف قيم العقد:
//a/text() #=> "Go home"
//a/@href #=> "index.html"
//a/* #=> All children of <a>
المسندات (Predicates)
المسندات (Predicates)
//div[true()]
//div[@class="head"]
//div[@class="head"][@id="top"]
تقوم بتصفية مجموعات العقد عند تطابق الشروط. يمكن ربط المسندات بشكل متسلسل.
المعاملات
# Comparison
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
# Logic (and/or)
//div[@id="head" and position()=2]
//div[(x and y) or not(z)]
استخدام العقد داخل المسندات
# Inside functions
//ul[count(li) > 2]
//ul[count(li[@class='hide']) > 0]
# Returns <ul> only if it has an <li> child
//ul[li]
الفهرسة
//a[1] # First <a> (1-based index)
//a[last()] # Last <a>
//ol/li[2] # Second <li>
//ol/li[position()=2] # Same as above
//ol/li[position()>1] # All except the first
ترتيب السلسلة
a[1][@href='/']
a[@href='/'][1]
الترتيب مهم: الأول يأخذ العنصر الأول ويتحقق من href الخاص به؛ الثاني يصفي حسب href أولاً ثم يختار أول تطابق.
المسندات المتداخلة
//section[.//h1[@id='hi']]
يُرجع عناصر <section> التي تحتوي على حفيد <h1> له السمة id='hi'.
الدوال
دوال العقد
name() # Starts with: //[starts-with(name(), 'h')]
text() # Exact match: //button[text()="Submit"]
# Value: //button/text()
lang(str)
namespace-uri()
count() # //table[count(tr)=1]
position() # //ol/li[position()=2]
الدوال المنطقية
not(expr) # //button[not(starts-with(text(),"Submit"))]
دوال النصوص
contains() # //span[contains(@class,"head")]
starts-with() # //span[starts-with(@class,"head")]
ends-with() # //span[ends-with(@class,"head")]
concat(x, y)
substring(str, start, len)
substring-before("01/02", "/") #=> "01"
substring-after("01/02", "/") #=> "02"
translate()
normalize-space()
string-length()
تحويل الأنواع
string()
number()
boolean()
المحاور
استخدام المحاور
//ul/li # ul > li
//ul/child::li # ul > li (explicit child axis)
//ul/following-sibling::li # ul ~ li
//ul/descendant-or-self::li # ul li
//ul/ancestor-or-self::li # $('ul').closest('li')
صياغة المحور:
// |
ul |
/child:: |
li |
|---|---|---|---|
| المحور | الخطوة | المحور | الخطوة |
محاور أخرى
| المحور | الاختصار | الوصف |
|---|---|---|
ancestor |
العناصر الأسلاف | |
ancestor-or-self |
العقدة الحالية وأسلافها | |
attribute |
@ |
مختصر attribute::href |
child |
المحور الافتراضي: div هي child::div |
|
descendant |
العقد الأحفاد | |
descendant-or-self |
// |
// هو اختصار لـ /descendant-or-self::node()/ |
namespace |
عقد فضاء الأسماء (Namespaces) | |
self |
. |
. هو اختصار لـ self::node() |
parent |
.. |
.. هو اختصار لـ parent::node() |
following |
كل شيء بعد وسم الإغلاق | |
following-sibling |
العقد الشقيقة بعد العنصر الحالي | |
preceding |
كل شيء قبل وسم الفتح | |
preceding-sibling |
العقد الشقيقة قبل العنصر الحالي |
الاتحادات (Unions)
//a | //span
استخدم | لدمج مجموعتي عقد أو أكثر في اتحاد واحد.
أمثلة
أنماط استعلام شائعة
//* # All elements
count(//*) # Count all elements
(//h1)[1]/text() # Text of the very first h1 heading in document
//li[span] # Find <li> containing a <span> child
//ul/li/.. # Navigate back to parent <ul>
إيجاد العناصر الأب
//section[h1[@id='section-name']]
يعثر على <section> يحتوي على ابن مباشر h1#section-name.
//section[.//h1[@id='section-name']]
يعثر على <section> يحتوي على أي حفيد h1#section-name.