dqczgtem06898
dqczgtem06898
2015-09-18 18:42
浏览 316

XPath根据属性值定位元素?

I am trying to search a XML document I created but I can't seem to get it to work properly. Here is my XML document...

<?xml version="1.0" encoding="UTF-8"?>
<states>
<repstate location="AL">
    <compName>Test Company</compName>
    <name>Chris Brown</name>
    <address>123 Main St</address>
    <city>Anytown</city>
    <state>CT</state>
    <phone>203-123-4567</phone>
    <fax>203-456-7890</fax>
    <email>info@email.com</email>
    <website>www.testing.com</website>
    <serviceArea>All of Alabama</serviceArea>
</repstate>
<repstate location="AK">
    <compName>Test Company</compName>
    <name>Chris Brown</name>
    <address>123 Main St</address>
    <city>Anytown</city>
    <state>CT</state>
    <phone>203-123-4567</phone>
    <fax>203-456-7890</fax>
    <email>info@email.com</email>
    <website>www.testing.com</website>
    <serviceArea>All of Alabama</serviceArea>
</repstate>
</states>

and here is the code snippet i'm trying to get to work...

        <?php 
            $xml = simplexml_load_file('../../usa.xml');
            $found = $xml->xpath("//@location='AK'");
            echo $found->state;
        ?>  

It doesn't seem to be working properly.

I'm trying to load the information in that node. So all the compName, name, address, city, state, phone, fax, email, website and serviceArea. I thought it would be easy to just get "state" working but nothing seems to output. What am I doing wrong?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试搜索我创建的XML文档,但似乎无法让它正常工作。 这是我的XML文档......

 &lt;?xml version =“1.0”encoding =“UTF-8”?&gt; 
&lt; states&gt; 
&lt; repstate  location =“AL”&gt; 
&lt; compName&gt;测试公司&lt; / compName&gt; 
&lt;名称&gt; Chris Brown&lt; / name&gt; 
&lt;地址&gt; 123 Main St&lt; / address&gt; 
&lt; city&gt;  Anytown&lt; / city&gt; 
&lt; state&gt; CT&lt; / state&gt; 
&lt; phone&gt; 203-123-4567&lt; / phone&gt; 
&lt; fax&gt; 203-456-7890&lt; / fax&gt; 
&lt; 电子邮件&gt; info@email.com< / email&gt; 
&lt; website&gt; www.testing.com&lt; / website&gt; 
&lt; serviceArea&gt;所有Alabama&lt; / serviceArea&gt; 
&lt; / repstate&gt; 
&lt; repstate location  =“AK”&gt; 
&lt; compName&gt;测试公司&lt; / compName&gt; 
&lt;名称&gt; Chris Brown&lt; / name&gt; 
&lt;地址&gt; 123 Main St&lt; / address&gt; 
&lt; city&gt; Anytown&lt  ; / city&gt; 
&lt; state&gt; CT&lt; / state&gt; 
&lt; phone&gt; 203-123-4567&lt; / phone&gt; 
&lt; fax&gt; 203-456-7890&lt; / fax&gt; 
&lt; email&gt  ; info@email.com< / email&gt; 
&  lt; website&gt; www.testing.com&lt; / website&gt; 
&lt; serviceArea&gt;所有Alabama&lt; / serviceArea&gt; 
&lt; / repstate&gt; 
&lt; / states&gt; 
   
  
 

这里是我试图开始工作的代码片段...

 &lt;?php 
 $ xml = simplexml_load_file('..  /../usa.xml');
 $ found = $ xml-&gt; xpath(“// @ location ='AK'”); 
 echo $ found-&gt; state; 
?&gt;  
   
 
 

它似乎无法正常工作。

我正在尝试加载该节点中的信息。 所以compName,名称,地址,城市,州,电话,传真,电子邮件,网站和serviceArea。 我认为让“状态”工作很容易,但似乎没有输出。 我做错了什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanshai4484
    duanshai4484 2015-09-20 19:45
    已采纳

    As Micheal Kay has outlined in his answer, the xpath expression is valid. What he does not know is that for SimpleXML this won't lead into an empty result.

    So you see a fatal error as you try to read a field from an empty array. Your code works similar to this one:

    $found = array();
    echo $found->state;
    

    Which echoes an empty string (created out of the NULL value that expression give) and a notice:

    Notice: Trying to get property of non-object in ...

    If you don't see yet this notice, it's crucial you enable error messages with your development system, see How to get useful error messages in PHP?.

    So as you can see, it is working properly, it just does not provide what you expect. To go on with that in a more productive manner for yourself, it would have been great if you would have shared what exactly you did expect instead.

    Or in other words: As others have answered as well, you need to decide what you're looking for.

    From the output code:

    echo $found->state;
    

    I'd say you're looking for the following XML element:

    ...
    <repstate location="AK">
        <compName>Test Company</compName>
        <name>Chris Brown</name>
        <address>123 Main St</address>
        <city>Anytown</city>
        <state>CT</state>
        ...
    

    So here is how SimpleXML Xpath works:

    1. you can only query lists of nodes
    2. the only nodes supported are elements and attributes
    3. the list is always of type array
    4. the array is always zero-indexed
    5. each entry in the array - if the array is not empty - is of type SimpleXMLElement

    So in your case if my assumption of the element to query you expect to get is correct, you want the state child-element's node value of a single element node returned. The according PHP code then would be:

    $list = $xml->xpath("//repstate[@location = 'AK']");
    list($repstate) = $list + array(null);
    $state = $repstate ? $repstate->state : '- no such location -';
    
    echo $state;
    

    The output with your XML then would be:

    CT

    Or even shorter:

    $list = $xml->xpath("//repstate[@location = 'AK']/state");
    list($state) = $list + array('- no such location -');
    
    echo $state;
    

    I hope this more expansive explanation is helpful for you. The reason why your original expression does not work is that it's already violating the first point:

    1. you can only query lists of nodes

    because your xpath query represented a boolean expression and not a node-list. Additionally such a list in SimpleXMLElement xpath would need you to have an element or attribute and not a boolean expression.

    点赞 评论
  • dongque3797
    dongque3797 2015-09-18 18:49

    I am not 100% positive on this, but in c# the xPath would be:
    //repstate[@location='AK']
    so you can try that (I can't put that in a comment as I don't have a high enough reputation :))

    点赞 评论
  • donglu9445
    donglu9445 2015-09-18 20:13

    You haven't said what you want your XPath expression to do. Your expression is valid, it will return a boolean which will be true if there is an @location attribute equal to "AK" anywhere in the document.

    If you want to find the element that has an @location attribute equal to "AK", use //*[@location='AK'].

    点赞 评论
  • douhui4699
    douhui4699 2015-09-18 21:30

    In addition to the xpath-expression itself, xpath will return an array of SimpleXML elements to $found, so...

    echo $found[0]->state;
    

    or - with PHP >= 5.4 - you can de-reference the array like this:

    $found = $xml->xpath(...)[0];
    echo $found->state;
    

    see it here: https://eval.in/435965

    点赞 评论

相关推荐