xpath查询是否具有像mysql这样的Limit选项 [英] Does xpath query has Limit option like mysql
问题描述
我想限制从xpath查询中收到的结果的数量.
I want to limit number of result I receive from xpath query.
例如:-
$info = $xml->xpath("//*[firstname='Sheila'] **LIMIT 0,100**");
您看到的是LIMIT 0,100
.
推荐答案
您应该可以使用"//*[firstname='Sheila' and position() <= 100]
"
给出以下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>
您可以使用以下XPath来获取前三个城市:
You can use the following XPath to get the first three cities:
//cityList/*[position()<=3]
结果:
Node element0 Abu
Node element1 Agartala
Node element2 Agra
如果要将其限制为以element
开头的节点:
If you want to limit this to nodes that start with element
:
//cityList/*[substring(name(), 1, 7) = 'element' and position()<=3]
请注意,后一个示例之所以有效,是因为您选择了Position()
可以按预期限制结果.如果cityList节点下混合了其他节点名称,则会得到不希望的结果.
例如,如下更改XML:
Note that this latter example works because you're selecting all the child nodes of cityList, so in this case Position()
works to limit the results as expected. If there was a mix of other node names under the cityList node, you'd get undesirable 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>
并使用上面的XPath表达式,我们现在获得
and using the above XPath expression, we now get
Node element0 Abu
请注意,我们丢失了第二和第三结果,因为position()
函数以更高的优先级进行求值-就像请求给我前三个节点,现在从这些节点中给了我所有的三个节点一样"以"element"开头的节点."
Note that we're losing the second and third results, because the position()
function is evaluating at a higher order of precedence - the same as requesting "give me the first three nodes, now out of those give me all the nodes that start with 'element'".
这篇关于xpath查询是否具有像mysql这样的Limit选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!