根据属性选择一个节点,然后在php中返回兄弟节点属性

根据属性选择一个节点,然后在php中返回兄弟节点属性

问题描述:

I am trying to get the content of the field[@value] attribute when the sibling node has field[@value='SIM Identification (ICCID)'. I need to do this with multiple xml docs and the parent node is not always located in the same array position so i need to use relative paths.

here is an example of the xml doc.

     <views>
       <view name="Summary">
         <item>
           <field name="Subject" value="Date Created" class=""/>
           <field name="Data" value="6/24/2013 11:06:54 AM" class=""/>
         </item>
       <view name="Case Data">
         <item>
           <field name="Subject" value="Case Reference" class=""/>
         </item>
       <view name="General Information">
         <item>
           <field name="Attribute" value="Device Name" class=""/>
           <field name="Data" value="SIM Card" class=""/>
         </item>
         <item>
           <field name="Attribute" value="SIM Identification (ICCID)" class=""/>
           <field name="Data" value="8935301120426207248" class=""/>
         </item>
       </view>
     </views>

Here is what I have come up with so far but can't quite get it to work.

    $doc = new DOMDocument;
    $doc->load('../SIMxml/207248.xml');
    $xpath = new DOMXPath($doc);

    $SIM_num_query = "/views/view[@name='General Information']/item [field[@name='Attribute' and @value='SIM Identification (ICCID)' and preceding-sibling::field[@value]]]/";

    echo $xpath->query($SIM_num_query),"<br/>";

This does not work, any suggestions?

当sibling节点有字段[@value时,我试图获取字段[@value]属性的内容 ='SIM识别(ICCID)'。 我需要使用多个xml文档执行此操作,并且父节点并不总是位于相同的数组位置,因此我需要使用相对路径。 p>

这里是xml doc的示例。 p>

 &lt; views&gt; 
&lt; view name =“Summary”&gt; 
&lt; item&gt; 
&lt; field name =“Subject”value =“Date 创建“class =”“/&gt; 
&lt; field name =”Data“value =”6/24/2013 11:06:54 AM“class =”“/&gt; 
&lt; / item&gt; 
  &lt; view name =“案例数据”&gt; 
&lt; item&gt; 
&lt; field name =“Subject”value =“案例参考”class =“”/&gt; 
&lt; / item&gt; 
&lt;  ; view name =“General Information”&gt; 
&lt; item&gt; 
&lt; field name =“Attribute”value =“Device Name”class =“”/&gt; 
&lt; field name =“Data”value  =“SIM卡”类=“”/&gt; 
&lt; / item&gt; 
&lt; item&gt; 
&lt; field name =“Attribute”value =“SIM识别(ICCID)”class =“”/&gt  ; 
&lt; field name =“Data”value =“8935301120426207248”class =“”/&gt; 
&lt; / item&gt; 
&lt; / view&gt; 
&lt; / views&gt; 
  code>   pre> 
 
 

这是我到目前为止所提出的但却无法得到它 工作。 p>

  $ doc = new DOMDocument; 
 $ doc-&gt; load('../ SIMxml / 207248.xml'); 
 $ xpath = new  DOMXPath($ doc); 
 
 $ SIM_num_query =“/ views / view [@ name ='General Information'] / item [field [@ name ='Attribute'和@ value ='SIM Identification(ICCID)'和 preceding-sibling :: field [@value]]] /“; 
 
 echo $ xpath-&gt; query($ SIM_num_query),”&lt; br /&gt;“; 
  code>  pre>  
 
 

这不起作用,有什么建议吗? p> div>

If it is always the following field you are looking for try this:

"//field[@value='SIM Identification (ICCID)']/following-sibling::field/@value"

If it the position is not fix you may try something like this:

"//field[@value='SIM Identification (ICCID)']/../field[@name='Data']/@value"