首页 > 解决方案 > 一种基于唯一 ID 限制数据的方法

问题描述

我有一个包含火车列表的 xml 文件,每列火车都与特定的 trainID 相关联。我编写了一个 php 脚本来将此数据编译为用户可读的格式,但无法找到一种方法来设置它以仅读取和报告与 trainID 关联的数据。我希望通过使用火车 ID 的 url 链接到火车报告页面。我尝试制作一个功能,但我认为这不是我需要的,因为它不起作用。下面是我的 XML 文件和列表生成器的一部分。

<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;
            }
        }
}
?>

标签: phpxmlxpath

解决方案


您可以在 XPath 表达式中添加一个条件来仅检索您想要的节点:

$railunit = $railunit->xpath("//TrainLoader[trainID = $trainid]")[0]; // retrieve all TrainLoader node that has a child 'trainID' with value '$trainid', then keep only the first one

// whole node
echo $railunit->asXML() ;

// access specific information
echo (string) $railunit->xpath('./ManuallyAppliedSpeedLimitMPH')[0]; // 2147483647

推荐阅读