Does xpath request have a Limit parameter like mysql

I want to limit the number of results retrieved from an xpath request.

For example: -

$info = $xml->xpath("//*[firstname='Sheila'] **LIMIT 0,100**"); 

You see that LIMIT 0,100 .

+8
xpath limit
source share
2 answers

You can use "//*[firstname='Sheila' and position() <= 100] "

Edit:

Given the following XML:

 <root> <country.php desc="country.php" language="fr|pt|en|in" editable="Yes"> <en/> <in> <cityList desc="cityList" language="in" editable="Yes" type="Array" index="No"> <element0>Abu</element0> <element1>Agartala</element1> <element2>Agra</element2> <element3>Ahmedabad</element3> <element4> Ahmednagar</element4> <element5>Aizwal</element5> <element150>abcd</element150> </cityList> </in> </country.php> </root> 

You can use the following XPath to get the first three cities:

 //cityList/*[position()<=3] 

Results:

 Node element0 Abu Node element1 Agartala Node element2 Agra 

If you want to limit this to nodes starting with element :

 //cityList/*[substring(name(), 1, 7) = 'element' and position()<=3] 

Note that this last example works because you select all cityList child nodes, so in this case Position() works to limit the results as expected. If there was a combination of other node names in the cityList node list, you would get unwanted results.
For example, changing the XML as follows:

 <root> <country.php desc="country.php" language="fr|pt|en|in" editable="Yes"> <en/> <in> <cityList desc="cityList" language="in" editable="Yes" type="Array" index="No"> <element0>Abu</element0> <dog>Agartala</dog> <cat>Agra</cat> <element3>Ahmedabad</element3> <element4> Ahmednagar</element4> <element5>Aizwal</element5> <element150>abcd</element150> </cityList> </in> </country.php> </root> 

and using the above XPath expression, now we get

 Node element0 Abu 

Note that we lose the second and third results, because the Position() function evaluates in a higher priority order - the same as the request "give me the first three nodes, now from them give me all the nodes that start with" item. "

+7
source share

Go into the same problem yourself, and I had some problem with Jeff's answer, because he, as he clearly describes, limits the number of returned items before he executes other parts of the request due to priority.

My solution is to add position() < 10 as an additional condition after applying other conditions, for example:

 //ElementsIWant[./ChildElementToFilterOn='ValueToSearchFor'][position() <= 10]/. 

Note that I use two separate conditional blocks. This will first filter out the elements that match my state, and secondly, only 10 of them will be taken.

0
source share

All Articles