douying7289
2017-04-12 18:17
浏览 71
已采纳

PHP和XPath查询

I have an XML file similar to the text below:

<?xml version="1.0" standalone="yes"?>
<Calendar xmlns="urn:AvmInterchangeSchema-Calendario-1.0">
  <Date>
    <Day>12/04/2017</Day>
    <TypesDay>
      <Type>Test 1</Type>
      <Type>Test 2</Type>
      <Type>Test 3</Type>
    </TypesDay>
  </Date>
</Calendar>

And I am using this Xpath to select the nodes:

$xml = simplexml_load_file("file.xml");
$response = $xml->xpath('//*[text()="'.date("d/m/Y").'"]');

How can I take the "TypesDay" entries if that condition is met?

I hope not to create duplicates ... I'm going crazy for hours and it will definitely be a trivial thing.

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

我有一个类似于下面文字的XML文件:

 &lt;?xml version =”1.0“standalone =”yes“?&gt; 
&lt; Calendar xmlns =”urn:AvmInterchangeSchema-Calendario-1.0“&gt; 
&lt; Date&gt  ; 
&lt; Day&gt; 12/04/2017&lt; / Day&gt; 
&lt; TypesDay&gt; 
&lt; Type&gt; Test 1&lt; / Type&gt; 
&lt; Type&gt; Test 2&lt; / Type&gt; 
&lt; 输入&gt;测试3&lt; /类型&gt; 
&lt; / TypesDay&gt; 
&lt; / Date&gt; 
&lt; / Calendar&gt; 
   
 
 

我正在使用此 用于选择节点的Xpath:

  $ xml = simplexml_load_file(“file.xml”); 
 $ response = $ xml-  &GT;的xpath( '// * [文本()= “' 日期( ”d / M / Y“)。” ']'); 
   
 
 

如果符合条件,我该如何处理“TypesDay”条目?

我希望不要创建重复项...我会疯狂几个小时,这肯定是一件微不足道的事情 。

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

1条回答 默认 最新

  • doucai6663 2017-04-12 19:02
    已采纳

    There are a few approaches to do this. First of all, you should register the namespace:

    $xml->registerXPathNamespace('x', 'urn:AvmInterchangeSchema-Calendario-1.0');
    

    I'm assuming that your node name with the value 12/04/2017 could change.

    First

    Find a node called TypesDay inside the named namespace x that the parent node has a child node with value 12/04/2017

    $response = $xml->xpath('//*[*[text()="'.date("d/m/Y").'"]]/x:TypesDay');
    

    Second

    Find a node called TypesDay inside the named namespace x that is sibling of node with value 12/04/2017

    $response = $xml->xpath('//*[text()="'.date("d/m/Y").'"]/following-sibling::x:TypesDay');
    

    The result for both is:

    array(1) {
      [0]=>
      object(SimpleXMLElement)#2 (1) {
        ["Type"]=>
        array(3) {
          [0]=>
          string(6) "Test 1"
          [1]=>
          string(6) "Test 2"
          [2]=>
          string(6) "Test 3"
        }
      }
    }
    

    After all, if you want only the entries, just add the next level /x:Type:

    $response = $xml->xpath('//*[*[text()="'.date("d/m/Y").'"]]/x:TypesDay/x:Type');
    

    Or:

    $response = $xml->xpath('//*[text()="'.date("d/m/Y").'"]/following-sibling::x:TypesDay/x:Type');
    

    Result:

    array(3) {
      [0]=>
      object(SimpleXMLElement)#3 (1) {
        [0]=>
        string(6) "Test 1"
      }
      [1]=>
      object(SimpleXMLElement)#4 (1) {
        [0]=>
        string(6) "Test 2"
      }
      [2]=>
      object(SimpleXMLElement)#5 (1) {
        [0]=>
        string(6) "Test 3"
      }
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题