XML with xpath and PHP: How to access the text value of an attribute of an entry

后端 未结 2 972
情话喂你
情话喂你 2020-12-12 03:43

xml:


  

        
相关标签:
2条回答
  • 2020-12-12 04:16

    xpath() returns an array, you have to select the first element of that array, at index 0. Attention: if there's no match, it may return an empty array. Therefore, you should add an if (isset($xpath[0])) clause, just in case.

    foreach ($xml->entry as $entry)
    {
        $xpath = $entry->xpath('./link[@rel="alternate"]/@href');
    
        if (isset($xpath[0]))
        {
            echo $xpath[0], "\n";
        }
    }
    
    0 讨论(0)
  • 2020-12-12 04:28

    You were close:

    ./link[@rel='alternate']/@href
    

    Should be the correct XPath to get those values.

    0 讨论(0)
提交回复
热议问题