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

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条回答 默认 最新

相关推荐 更多相似问题