0

PHP XPath query:

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

Targeted 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

How can I alter the PHP so that I also retrieve the Reg for each one as well? The Service and Reg have to be inside the same Vehicle when retrieved (not random pairs).

jskidd3
  • 4,609
  • 15
  • 63
  • 127
  • Your question is not really clear. According to the XML structure, the Reg should be part of `textContent` already. – hakre Sep 29 '13 at 12:49

1 Answers1

3

How about querying vehicle and then using it as context node to search for Service and Reg?

$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),
    );
}

See as well:

Community
  • 1
  • 1
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85