0

I have tried to parse an XML response that does not work. In the API docs it says that it is a XML based protocol: SRU. Wont this XML parsing code work then? Or is it some error in the code?

code:

$xmldoc = new DOMDocument();
$xmldoc->load('http://api.site.com/sru/query=' . $_GET['q']);
print_r($xmldoc);

$xpathvar = new Domxpath($xmldoc);
$queryResult = $xpathvar->query('//datafield[@tag="020"]/subfield[$code="a"]');
foreach ($queryResult as $result) {
    echo $result->textContent;
    print_r($result);
}

xml structure:

<searchRetrieveResponse>
    <numberOfRecords>1</numberOfRecords>
    <datafield tag="020" ind1=" " ind2=" ">
        <subfield code="a">xyz</subfield>
    </datafield>

    ...

</searchRetrieveResponse>
Paul DelRe
  • 4,003
  • 1
  • 24
  • 26
Joseph
  • 1,734
  • 6
  • 29
  • 51

1 Answers1

0

The dollar sign in $code (in the xpathvar->query) looks like it doesn't belong there. Try either removing or escaping the dollar sign, as appropriate in your case (removing looks appropriate, given the example xml).

eykanal
  • 26,437
  • 19
  • 82
  • 113
  • I though this was only true for double quotes. Perhaps it is version-dependent? – jisaacstone May 04 '11 at 00:55
  • No, you're correct... this is incorrect as it is now. I'm editing it now. – eykanal May 04 '11 at 01:11
  • @eyekanal Sry my bad with the question posting. I edited the attribute for easier reading and put $ instead of @. The code has the correct syntax but is still not passing anything to $queryResults. The foreach loop does not go. – Joseph May 04 '11 at 06:59