Does xpath query has Limit option like mysql

前端 未结 2 1578
余生分开走
余生分开走 2020-12-19 02:21

I want to limit number of result I receive from xpath query.

For example:-

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


        
2条回答
  •  鱼传尺愫
    2020-12-19 02:49

    You should be able to use "//*[firstname='Sheila' and position() <= 100]"

    Edit:

    Given the following XML:

     
         
             
             
                 
                    Abu
                    Agartala
                    Agra 
                    Ahmedabad
                     Ahmednagar 
                    Aizwal
                    abcd 
                 
             
         
    
    

    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 that start with element:

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

    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:

     
         
             
             
                 
                    Abu
                    Agartala
                    Agra 
                    Ahmedabad
                     Ahmednagar 
                    Aizwal
                    abcd 
                 
             
         
    
    

    and using the above XPath expression, we now get

    Node    element0    Abu
    

    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'".

提交回复
热议问题