使用DOMXPath用XPath表达式替换foreach循环

我想用xpath表达式替换foreach循环,但是我需要一个DOMXPath对象来返回多个 list。</ p>

我有以下XML(简化)和我使用 DOMDocument DOMXPath 迭代它: </ p>

 &lt; a:RoomsType&gt; 
&lt; a:Rooms&gt;
&lt; a:Room&gt;
&lt; a:RPH&gt; 0&lt; / a:RPH&gt ;
&lt; / a:房间&gt;
&lt; a:房间&gt;
&lt; a:RPH&gt; 1&lt; / a:RPH&gt;
&lt; / a:房间&gt;
&lt; a:房间&gt;

&lt; a:RPH&gt; 2&lt; / a:RPH&gt;
&lt; / a:房间&gt;
&lt; a:房间&gt;
&lt; a:RPH&gt; 0&lt; / a:RPH&gt;
&lt; ; / a:房间&gt;
&lt; a:房间&gt;
&lt; a:房间类型&gt;
</ code> </ pre>

我想按RPH编号拆分房间,创建 每个RPH的房间列表 数。 目前,我正在使用以下代码:</ p>

  // $ xpath是一个DOMXPath对象
$ roomsToIterate = $ this-&gt; xpath-&gt; query(“/ / a:RoomsType / a:Rooms / a:Room“);

$ roomList = array();
foreach($ roomsToIterate as $ room){
$ rphCandidate = $ room-&gt; getElementsByTagName(” RPH“) - &gt; item(0) - &gt; nodeValue;
if(!isset($ roomList [$ rphCandidate])){
$ roomList [$ rphCandidate] = array();
}
$ roomList [$ rphCandidate] [] = $ room;
}
</ code> </ pre>

这个现在正在运行,但我想用Xpath表达式替换foreach循环。 我可以使用表达式 $ rooms = $ this-&gt; xpath-&gt; query(“// a:RoomsType / a:Rooms / a:Room [a:RPH ='{$ rph}']”) ; </ code>, $ rph </ code>是一个数字,但是如果我不知道RPH(如果它可以是0到99之间的任何东西),我怎么能这样做呢。 是否可能?</ p>

简而言之,有没有办法用XPath替换我的foreach循环? </ p>

我在考虑使用 registerPhpFunctions < / a>和一个自定义函数,但我关注这种方法与foreach循环相比的性能</ p>
</ div>

展开原文

原文

I want to replace a foreach loop using a xpath expression, but I need that a DOMXPath object to return more than one list.

I have the following XML (simplified) and I using DOMDocument and DOMXPath to iterate over it:

<a:RoomsType>
    <a:Rooms>
      <a:Room>
          <a:RPH>0</a:RPH>
      </a:Room>
      <a:Room>
          <a:RPH>1</a:RPH>
      </a:Room>
      <a:Room>
          <a:RPH>2</a:RPH>
      </a:Room>
      <a:Room>
          <a:RPH>0</a:RPH>
      </a:Room>
    <a:Rooms>
<a:RoomsType>

I want to split the rooms by the RPH number, creating a list of rooms for each RPH number. Currently, I'm using the following code:

 //$xpath is a DOMXPath object
 $roomsToIterate = $this->xpath->query("//a:RoomsType/a:Rooms/a:Room");

 $roomList = array();
 foreach ($roomsToIterate as $room) {
     $rphCandidate = $room->getElementsByTagName("RPH")->item(0)->nodeValue;
     if (!isset($roomList[$rphCandidate])) {
         $roomList[$rphCandidate] = array();
     }
     $roomList[$rphCandidate][] = $room;
 }

This is working for now, but I want to replace the foreach loop with a Xpath expression. I can use the expression $rooms = $this->xpath->query("//a:RoomsType/a:Rooms/a:Room[a:RPH='{$rph}']"); with $rph being a number, but how can I do it if I don't know the RPH (it could be anything between 0 and 99). Is it possible?

In short, Are there any way to replace my foreach loop using XPath?

I was thinking about the use of registerPhpFunctions and a custom function, but I concerned about the performance of this approach compared with foreach loop

doushu2699
doushu2699 谢谢你的评论。所以,我的原始方法是正确的。我将保持这个问题的任何新改进想法,但如果你愿意,你可以发表评论作为答案。如果我没有收到任何新建议,我会将其标记为正确。
接近 4 年之前 回复
douzheyo2617
douzheyo2617 不,你还需要一个foreach。您必须迭代房间以创建嵌套结果或可能的$ph值以获取组。
接近 4 年之前 回复

1个回答



Xpath 1.0表达式将返回一个节点列表,如果使用像这样的轴,它们可以在某种程度上展平现有结构 后代</ code>或 ancestor </ code>,但它将是一个节点列表。 它无法对它们进行分组或聚合。</ p>

您可以获取具有特定 RPH </ code>值的节点列表。 但是你需要对每个值进行此操作,结果将是另一个循环。 这意味着获取所有 RPH </ code>值,使它们唯一,迭代它们并执行每个值的Xpath表达式。</ p>

您当前的解决方案没问题。</ p>
</ div>

展开原文

原文

Xpath 1.0 expression will return a list of nodes, they can to some extend flatten an existing structure if you use an axis like descendant or ancestor, but it will be a list of nodes. It can not group or aggregate them.

You could fetch a lists of nodes with a specific RPH value. But you would need to this for each value, the result would be another loop. This would mean to fetch all RPH values, make them unique, iterate them and execute and Xpath expression for each value.

Your current solution is fine.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐