dqczgtem06898 2015-09-18 18:42
浏览 422
已采纳

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?

  • 写回答

4条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?