If I understand you correctly you would like to check for the last element in each car
element node? Well, Xpath hast two methods position()
and last()
that can be used in a condition.
Select the car nodes
/details/car
Select the child element nodes of the car
nodes
/details/car/*
Add a condition to limit the selection to the last node
/details/car/*[last()]
Full example: https://eval.in/145531
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
foreach ($xpath->evaluate('/details/car/*[last()]') as $node) {
var_dump(
$node->nodeName,
$node->nodeValue
);
}
Output:
string(9) "Jan-31-14"
string(1) "0"
HINT!
Flexible element names are really bad style, you will not be able to define them in a schema. If possible I suggest you change them to something like <amount date="Jan-31-14">0</amount>