I have a xml file with a listing of trains and each train make up is tied to a specific trainID. I wrote a php script to compile this data into a user readable format but can't figure out a way to set it up to read and report only the data that is associated with the trainID. Im looking to link to the train report page with a url using the train ID. I tried making a function but I don't think that is what i need, as it was not working. Below is a portion of my XML file and list generator.
<ScnLoader xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<trainList>
<TrainLoader>
<trainID>99991</trainID>
<TrainWasAI>false</TrainWasAI>
<DispatchTrainDirection>0</DispatchTrainDirection>
<ManuallyAppliedSpeedLimitMPH>2147483647</ManuallyAppliedSpeedLimitMPH>
<PreviousSignalInstruction>Clear</PreviousSignalInstruction>
<unitLoaderList>
<RailVehicleStateClass>
<rvXMLfilename>R8_CoveredHopper_PS4750_DGHX01.xml</rvXMLfilename>
<unitType>US_Freightcar</unitType>
<currentRoutePrefix>
<int>320</int>
<int>320</int>
</currentRoutePrefix>
<currentTrackSectionIndex>
<int>1365</int>
<int>1365</int>
</currentTrackSectionIndex>
<startNodeIndex>
<int>0</int>
<int>0</int>
</startNodeIndex>
<distanceTravelledInMeters>
<float>76.0736</float>
<float>90.00746</float>
</distanceTravelledInMeters>
<reverseDirection>
<boolean>true</boolean>
<boolean>true</boolean>
</reverseDirection>
<loadWeightUSTons>111.1</loadWeightUSTons>
<destinationTag>BAR HOU</destinationTag>
<unitNumber>571555</unitNumber>
</RailVehicleStateClass>
</unitLoaderList>
</TrainLoader>
</trainList>
</ScnLoader>
php
<?php
$railunit = simplexml_load_file('railUnitList.xml'); //database of railunits
$orders = simplexml_load_file('testdata.xml'); //Where the data comes from to form the list
?><pre><?php print_r($orders); ?></pre><?php
foreach ($orders->TrainLoader->trainID as $trainID){
$trainid = "99991";
}
foreach ($orders->xpath("RailVehicleStateClass") as $traininfo) {
$rvXMLfilename=(string)$traininfo->rvXMLfilename;
$unitType=(string)$traininfo->unitType;
$unitNumber=(int)$traininfo->unitNumber;
$destinationTag=(string)$traininfo->destinationTag;
$loadWeightUSTons=(int)$traininfo->loadWeightUSTons;
$totalUnitCount = $totalUnitCount + 1;
echo "<tr>";
echo "<td align='center'>";
echo $totalUnitCount;
echo "</td>";
echo "<td>";
foreach ($railunit->railUnit as $ru) {
if((string)$ru->rvXMLfilename == $rvXMLfilename){
$message = (string)$ru->reportingMark;
}
}
echo $message;
echo "</td>";
echo "<td>";
echo $unitNumber;
echo "</td>";
echo "<td>";
$message = "Not Found!";
foreach ($railunit->railUnit as $ru) {
if((string)$ru->rvXMLfilename == $rvXMLfilename){
$message = (string)$ru->unitType;
}
}
}
?>
You can add a condition in your XPath expression to retrieve only the node you want :