doucuodan0897 2016-11-15 18:00
浏览 100
已采纳

在变量中添加额外的字符串 - PHP

Here's my problem. I want to create a function that takes an outside variable that contains an xpath and once the function runs I want to add to that same variable to create a counter.

So I have the outside variable:

 $node = $xmlDoc->xpath('//a:Order');

Then the function with a single argument that will take the outside variable ($node). Like so:

function loopXML($node) {
    i=1; //counter variable
}

Now I want to add a counter to $node so that it goes through all of the children of "Order". Outside of the function, I would use:

$child = $xmlDoc->xpath('//a:Order['.$i.']/*'); 

But inside of the function, I have no idea how to concat it. Does anyone have any idea how I could do this?

EDIT: Also, it should be noted that I created an arbitrary namespace already:

 foreach($xmlDoc->getDocNamespaces() as $strPrefix => $strNamespace) {
     if(strlen($strPrefix)==0) {
         $strPrefix="a"; //Assign an arbitrary namespace prefix.
     }
     $xmlDoc->registerXPathNamespace($strPrefix,$strNamespace);
 }
  • 写回答

2条回答 默认 最新

  • dongyou4411 2016-11-15 18:17
    关注

    SimpleXMLElement::xpath() uses the node associated with the SimpleXML element as the context so you can do something like:

    foreach ($xmlDoc->xpath('//a:Order') as $order) {
      foreach ($order->xpath('*') as $field) {
        ...
      }
    } 
    

    But SimpleXMLElement::children() is a list of the element child nodes so it returns the same as the Xpath expression * or to be more exact '*[namespace-uri == ""]'. The first argument is the namespace of the children you would like to fetch.

    foreach ($xmlDoc->xpath('//a:Order') as $order) {
      foreach ($order->children() as $field) {
        ...
      }
    }
    

    This can be easily refactored into a function.

    function getRecord(SimpleXMLelement $order, $namespace) {
      $result = [];
      foreach ($order->children($namespace) as $field) {
        $result[$field->getName()] = (string)$field;
      }
      return $result;
    }
    

    You should always depend on the actual namespace, never on the prefix. Prefixes can change and are optional.

    Put all together:

    $xml = <<<'XML'
    <a:orders xmlns:a="urn:a">
      <a:order>
        <a:foo>bar</a:foo>
        <a:answer>42</a:answer>
      </a:order>
    </a:orders>
    XML;
    
    $namespace = 'urn:a';
    
    $orders = new SimpleXMLElement($xml);
    $orders->registerXpathNamespace('a', $namespace);
    
    function getRecord(SimpleXMLelement $order, $namespace = NULL) {
      $result = [];
      foreach ($order->children($namespace) as $field) {
        $result[$field->getName()] = (string)$field;
      }
      return $result;
    } 
    
    foreach ($orders->xpath('//a:order') as $order) {
      var_dump(getRecord($order, $namespace));
    }
    

    Output:

    array(2) {
      ["foo"]=>
      string(3) "bar"
      ["answer"]=>
      string(2) "42"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址