Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在XPath中使用索引_Xpath_Indexing_Expression_Using - Fatal编程技术网

在XPath中使用索引

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

我有以下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
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>