我有一个具有以下结构的 XML 文件:
<r>
<a>
<i>
<k>1</k>
<d>d1</d>
</i>
<i>
<k>3</k>
<d>d3</d>
</i>
</a>
<b>
<i>
<k>1</k>
<d>rd1</d>
</i>
<i>
<k>2</k>
<d>id2</d>
</i>
<i>
<k>3</k>
<d>rd3</d>
</i>
</b>
</r>
我选择i下面的节点/r/a并迭代它们以查找下面的关联节点/r/b并检索其节点的值,d如下所示:
$data = [];
$nodes = $domXPath->evaluate('/r/a/i');
foreach($nodes as $node) {
$key = $domXPath->evaluate('string(k)', $node);
$data[] = $domXpath->evaluate("string(/r/b/i[k=$key]/d)", $node);
}
这会产生正确的结果$data:
[
"rd1",
"rd3",
]
我的问题是是否可以在不将密钥拔出 PHP 的情况下执行此操作,例如:
$data = [];
$nodes = $domXPath->evaluate('/r/a/i');
foreach($nodes as $node) {
$data[] = $domXpath->evaluate("string(/r/b/i[k=initial-context()/k]/d)", $node);
}
凤凰求蛊