在XPath中使用索引
我有以下XML片段:在XPath中使用索引,xpath,indexing,expression,using,Xpath,Indexing,Expression,Using,我有以下XML片段: <ul> <li>xxx <ul> enter code here <li>1</li> <li>2</li> </ul> </li> <li>yyy <ul> <li>3</li> <li>4</li>
<ul>
<li>xxx
<ul> enter code here
<li>1</li>
<li>2</li>
</ul>
</li>
<li>yyy
<ul>
<li>3</li>
<li>4</li>
</ul>
</li>
按预期返回xxx
、1
和2
。但是当我使用
(//ul[@class="some-class"][1]//li)[2]
它从1
开始返回,而不是像我预期的那样从yyy
开始返回。请告知。尝试XPath
//ul[@class='some-class'][1]/li[2]//text()[1]
它给
yyy
34
作为输出。如何处理空间是另一回事
(//ul[@class="some-class"][1]//li)[1]
当您使用//li
时,这意味着ul标签的所有下降
<ul>
<li>xxx # the 1st li tag
<ul> enter code here
<li>1</li> # the 2st li tag
<li>2</li> # the 3st li tag
</ul>
</li>
<li>yyy # the 4st li tag
<ul>
<li>3</li> # the 5st li tag
<li>4</li> # the 6st li tag
</ul>
</li>
<ul>
<li>xxx # the 1st li tag
<ul> enter code here
<li>1</li>
<li>2</li>
</ul>
</li>
<li>yyy # the 2st li tag
<ul>
<li>3</li>
<li>4</li>
</ul>
</li>
更新您的
xml
示例,因为没有具有class=“some class”
属性的ul
元素
<ul>
<li>xxx # the 1st li tag
<ul> enter code here
<li>1</li>
<li>2</li>
</ul>
</li>
<li>yyy # the 2st li tag
<ul>
<li>3</li>
<li>4</li>
</ul>
</li>