dougou8458 2013-06-19 18:54
浏览 228
已采纳

XPath如何查询下一个节点[重复]

Here is a sample of the xml File. If I have the id value of 20 which is halfway through the xml file. How do I first search that node out and second find the next (following) value.

<League>
  <Id>20</Id>
  <Name>Major League Soccer</Name>
  <Country>USA</Country>
  <Historical_Data>Partial</Historical_Data>
  <Fixtures>Yes</Fixtures>
  <Livescore>Yes</Livescore>
  <NumberOfMatches>135</NumberOfMatches>
  <LatestMatch>2013-06-16T04:00:00+02:00</LatestMatch>
</League>
<League>
  <Id>33</Id>
  <Name>Allsvenskan</Name>
  <Country>Sweden</Country>
  <Historical_Data>Partial</Historical_Data>
  <Fixtures>Yes</Fixtures>
  <Livescore>Yes</Livescore>
  <NumberOfMatches>88</NumberOfMatches>
  <LatestMatch>2013-06-15T16:00:00+02:00</LatestMatch>
</League>
</div>
  • 写回答

1条回答 默认 最新

  • douye1940 2013-06-19 19:10
    关注

    Since League with id = 20 is halfway through the XML file you can do it dynamically using XPath. Here's a simple PHP code snippet:

    <?php
    $xml = <<<XML
    <Leagues>
        <League>
            <Id>20</Id>
            <Name>Major League Soccer</Name>
            <Country>USA</Country>
            <Historical_Data>Partial</Historical_Data>
            <Fixtures>Yes</Fixtures>
            <Livescore>Yes</Livescore>
            <NumberOfMatches>135</NumberOfMatches>
            <LatestMatch>2013-06-16T04:00:00+02:00</LatestMatch>
        </League>
        <League>
            <Id>33</Id>
            <Name>Allsvenskan</Name>
            <Country>Sweden</Country>
            <Historical_Data>Partial</Historical_Data>
            <Fixtures>Yes</Fixtures>
            <Livescore>Yes</Livescore>
            <NumberOfMatches>88</NumberOfMatches>
            <LatestMatch>2013-06-15T16:00:00+02:00</LatestMatch>
        </League>
    </Leagues>
    XML;
    
    $sxe = new SimpleXMLElement($xml);
    
    // Retrieve league with Id = 20
    $league20 = $sxe->xpath("//League[Id='20']");
    print_r($league20);
    
    // Retrieve league right after the one with Id = 20
    $leagueAfter20 = $sxe->xpath("//League[Id='20']/following-sibling::League[1]");
    print_r($leagueAfter20);
    

    Output

    Array
    (
    [0] => SimpleXMLElement Object
        (
            [Id] => 20
            [Name] => Major League Soccer
            [Country] => USA
            [Historical_Data] => Partial
            [Fixtures] => Yes
            [Livescore] => Yes
            [NumberOfMatches] => 135
            [LatestMatch] => 2013-06-16T04:00:00+02:00
        )
    )
    Array
    (
    [0] => SimpleXMLElement Object
        (
            [Id] => 33
            [Name] => Allsvenskan
            [Country] => Sweden
            [Historical_Data] => Partial
            [Fixtures] => Yes
            [Livescore] => Yes
            [NumberOfMatches] => 88
            [LatestMatch] => 2013-06-15T16:00:00+02:00
        )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档