Reputation: 139
I have an XML document in the following format:
<CONFIGURATION>
<ADMIN REFERRER="BCTRD" REQUIRE_AGREEMENTS="true" ALLOW_UPGRADES="true" />
<LAYOUT HIDE_PRESELECTED_SERVICES="true" AGREEMENTS_PER_EXCHANGE="true"></LAYOUT>
<SERVICE ID="TRE" CODE="TRADER_EMINI" OPTIONAL_BROKER="true">
<EXCHANGE GROUP="FOO"/>
</SERVICE>
<SERVICE ID="TR" CODE="TRADER">
<DEFAULT/>
<EXCHANGE GROUP="BAR"/>
</SERVICE>
</CONFIGURATION>
I am currently using this code block to retrieve the service code of the default service:
$serviceList = $this->xpaths->query('config',"//SERVICE");
foreach ($serviceList as $service) {
if ($service->getElementsByTagName('DEFAULT')->length) {
$serviceCode = $service->attributes->getNamedItem('CODE')->nodeValue;
break;
}
}
I suspect that it's possible to do this with one or two lines of XPath, but I haven't been able to figure out how. How would you format an XPath query to retrieve the service code I'm after?
Upvotes: 0
Views: 48
Reputation: 4108
You can get the 'CODE' attribute's value directly by using the following XPath statement
//SERVICE[DEFAULT]/@CODE
This basically is 'give me the attribute CODE from any SERVICE element that has DEFAULT child element.
Hope this helps.
Upvotes: 1
Reputation: 6625
Another xpath:
//SERVICE/DEFAULT/../@CODE
see it working: http://codepad.viper-7.com/CCzCn0
Upvotes: 1