मान लें कि हमारे पास निम्नलिखित संरचना है:
some text
<img src="#" />
sdfsdf
sdfsdf
plaintext-identificator-123
<img src="1" /> //get
<img src="2" /> //get
<img src="3" /> //not get
मुझे "सादा टेक्स्ट-पहचानकर्ता-123" के बाद निकटतम 2 तत्व प्राप्त करने की आवश्यकता है।
इस मामले में, वांछित परिणाम srcs 1 और 2 के साथ 2 तत्व प्राप्त करना होगा।
1
Tadeáš Jílek
17 सितंबर 2020, 14:14
2 जवाब
सबसे बढ़िया उत्तर
यह तरीकों में से एक है:
//text()[contains(., 'plaintext-identificator-123')]/following-sibling::img[position()<3]
1
Alexey R.
17 सितंबर 2020, 14:50
इसे अपनी वास्तविक संरचना पर आज़माएं और देखें कि क्या यह काम करता है (यह प्रश्न में आपके नमूने के लिए काम करता है):
(//img['plaintext-identificator-123']/following-sibling::img)[position()<3]
यदि आप लक्ष्य img
नोड्स की src
विशेषता के विशेषता मान चाहते हैं, तो इसे इसमें बदलें:
(//img['plaintext-identificator-123']/following-sibling::img)[position()<3]/@src
संपादित करें:
नीचे दी गई टिप्पणियों के बाद, इसे आजमाएं:
(//text()[contains(.,'plaintext-identificator-123')]/preceding-sibling::img/following-sibling::img)[position()<3]/@src
http://xpather.com/DOJz7nl1 का उपयोग करें (इसे इंडेंटिफ़ायर के साथ और उसके बिना आज़माएं)।
1
Jack Fleeting
17 सितंबर 2020, 15:27