0

PHP XPath 查询:

$query = $xpathvar->query('//siri:Service');
foreach($query as $service) {
    echo $service->textContent;
    // Here I need to also echo this Vehicle's reg
}

目标 XML:

<Vehicle>
    <CategoryA>
        <Service>123123123</Service>
    </CategoryA>
    <CategoryB>
         <SubCategoryA>
             <Reg>ASDF_XC1</Reg>
         </SubCategoryA>
    </CategoryB>
</Vehicle>
// Imagine many more vehicles here each with a service and reg

我怎样才能改变 PHP 以便我也检索每个 Reg 呢?检索时服务和注册必须在同一车辆内(不是随机对)。

4

1 回答 1

3

如何查询车辆,然后将其用作上下文节点来搜索ServiceReg

$vehicles = $xpath->query('//siri:Vehicle');
$pairs = array();
foreach ($vehicles as $vehicle) {
    $pairs[] = array(
        'Service' => $xpath->evaluate('string(.//siri:Service)', $vehicle),
        'Reg'     => $xpath->evaluate('string(.//siri:Reg)'    , $vehicle),
    );
}

另见:

于 2013-09-29T12:37:25.313 回答