1

I've got an url that return an xml but I have some problem to extract "link" element.

<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  <channel>
    <item>
      <id>123</id>
      <title>my title</title>
      <link>
        http://example.org
      </link>
    </item>
  </channel>
</rss>

I need to test it with

Symfony\Component\DomCrawler\Crawler

These are my tests:

$crawler = $this->client->get('/my-feed');

$items = $crawler->filterXPath('//channel/item');
$this->assertGreaterThanOrEqual(1, $items->count()); // ok pass

// ...
$titles = $items->filterXPath('//title')->extract(array('_text'));
$this->assertContains("my title", $titles);  // ok pass


// ...
$links = $items->filterXPath('//link')->extract(array('_text'));
$this->assertContains("example.org", $links);  // KO!!! don't pass

var_dump($links); // empty string

"link" is a reserved word?

Dennais
  • 476
  • 5
  • 14
  • Could you check out what ``$items->filterXPath('//link')`` contains? – Rob Apr 17 '15 at 09:50
  • it return a crowler object: `code object(Symfony\Component\DomCrawler\Crawler)#1968 (5) { ["uri":protected]=> NULL ["defaultNamespacePrefix":"Symfony\Component\DomCrawler\Crawler":private]=> string(7) "default" ["namespaces":"Symfony\Component\DomCrawler\Crawler":private]=> array(0) { } ["baseHref":"Symfony\Component\DomCrawler\Crawler":private]=> NULL ["storage":"SplObjectStorage":private]=> array(1) { ["0000000049a37b5b000000002da5082f"]=> array(2) { ["obj"]=> object(DOMElement)#1970 (0) { } ["inf"]=> NULL } } }` – Dennais Apr 17 '15 at 10:01
  • your RSS xml code is broken – VMAtm Apr 17 '15 at 12:11

1 Answers1

2

Your XML is broken:

  1. you don't have a closing channel node </channel>
  2. you don't have a closing rss node </rss>

Here is corrected XML :

<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <item>
            <id>123</id>
            <title>my title</title>
            <link>http://example.org</link>
        </item>
    </channel>
</rss>

Then, ->extract() returns An array of extracted values. So you shouldn't directly try to see its contain but get the first element and do your test:

$this->assertContains("my title", $titles[0]);
// ...
$this->assertContains("example.org", $links[0]);
j0k
  • 22,600
  • 28
  • 79
  • 90
  • Yes, sorry I was wrong in copy paste code, Xml is correct with and Problem is only in $items->filterXPath('//link') other elements works – Dennais Apr 17 '15 at 12:58
  • 1
    @Vega I've tried with the xml I posted and using `[0]` and it worked for me.. Are you sure about your xml? What does `$this->client->getResponse()->getContent()` says after your `->get(`? – j0k Apr 17 '15 at 14:04
  • @Vega Glad to hear that! You can now accept the answer :) – j0k Apr 21 '15 at 15:05