简体   繁体   中英

Xpath in PHP DOMXPath Not Working

I can't get the following DOMXpath to work:

    $responseDOM = new DOMDocument();
    $responseDOM->loadXML($responsestring);
    $responseXPath = new DOMXpath($responseDOM);
    $responseXPath->registerNamespace("ns", "http://www.loc.gov/MARC21/slim");
    $xpatheval = $responseXPath->query('//record');
    d(get_class($xpatheval));
    d($xpatheval->length);

d() is a custom library function I used similar to var_dump. When I dump $xpatheval->length, I get 0, when it should be more than 0, as $xpatheval should contain the relevant XML elements.

This is $responsestring:

<searchRetrieveResponse xmlns="http://www.loc.gov/zing/srw/" xmlns:oclcterms="http://purl.org/oclc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:diag="http://www.loc.gov/zing/srw/diagnostic/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<version>1.1</version>
<numberOfRecords>641</numberOfRecords>
<records>
<record>
<recordSchema>marcxml</recordSchema>
<recordPacking>xml</recordPacking>
<recordData>
<record xmlns="http://www.loc.gov/MARC21/slim">
    <leader>00000cam a2200000M  4500</leader>
    <controlfield tag="001">957956968</controlfield>
    <controlfield tag="008">160712s2016    gw      o     000 0 eng d</controlfield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="a">9783319415406</subfield>
      <subfield code="q">E-Book</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="a">3319415409</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="z">9783319415390</subfield>
    </datafield>
    <datafield ind1="0" ind2="0" tag="245">
      <subfield code="a">Computer Aided Verification :</subfield>
      <subfield code="b">28th International Conference, CAV 2016, Toronto, ON, Canada, July 17-23, 2016, Proceedings, Part II /</subfield>
      <subfield code="c">edited by Swarat Chaudhuri, Azadeh Farzan.</subfield>
    </datafield>
    <datafield ind1=" " ind2="1" tag="264">
      <subfield code="a">Cham</subfield>
      <subfield code="b">Springer International Publishing</subfield>
      <subfield code="c">2016</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="300">
      <subfield code="a">1 Online-Ressource.</subfield>
    </datafield>
    <datafield ind1="1" ind2=" " tag="490">
      <subfield code="a">Lecture Notes in Computer Science</subfield>
      <subfield code="v">9780</subfield>
    </datafield>
    <datafield ind1="0" ind2=" " tag="490">
      <subfield code="a">SpringerLink : Bücher</subfield>
    </datafield>
    <datafield ind1=" " ind2="0" tag="650">
      <subfield code="a">Computer science.</subfield>
    </datafield>
    <datafield ind1=" " ind2="0" tag="650">
      <subfield code="a">Computer organization.</subfield>
    </datafield>
    <datafield ind1=" " ind2="0" tag="650">
      <subfield code="a">Software engineering.</subfield>
    </datafield>
    <datafield ind1=" " ind2="0" tag="650">
      <subfield code="a">Computer logic.</subfield>
    </datafield>
    <datafield ind1=" " ind2="0" tag="650">
      <subfield code="a">Logic, Symbolic and mathematical.</subfield>
    </datafield>
    <datafield ind1=" " ind2="7" tag="650">
      <subfield code="a">Computer logic.</subfield>
      <subfield code="2">fast</subfield>
      <subfield code="0">(OCoLC)fst00872265</subfield>
    </datafield>
    <datafield ind1=" " ind2="7" tag="650">
      <subfield code="a">Computer organization.</subfield>
      <subfield code="2">fast</subfield>
      <subfield code="0">(OCoLC)fst00872356</subfield>
    </datafield>
    <datafield ind1=" " ind2="7" tag="650">
      <subfield code="a">Computer science.</subfield>
      <subfield code="2">fast</subfield>
      <subfield code="0">(OCoLC)fst00872451</subfield>
    </datafield>
    <datafield ind1=" " ind2="7" tag="650">
      <subfield code="a">Logic, Symbolic and mathematical.</subfield>
      <subfield code="2">fast</subfield>
      <subfield code="0">(OCoLC)fst01002068</subfield>
    </datafield>
    <datafield ind1=" " ind2="7" tag="650">
      <subfield code="a">Software engineering.</subfield>
      <subfield code="2">fast</subfield>
      <subfield code="0">(OCoLC)fst01124185</subfield>
    </datafield>
    <datafield ind1="1" ind2=" " tag="700">
      <subfield code="a">Chaudhuri, Swarat,</subfield>
      <subfield code="e">Herausgeber.</subfield>
      <subfield code="4">edt</subfield>
    </datafield>
    <datafield ind1="1" ind2=" " tag="700">
      <subfield code="a">Farzan, Azadeh,</subfield>
      <subfield code="e">Herausgeber.</subfield>
      <subfield code="4">edt</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="856">
      <subfield code="u">http://sfx.ethz.ch/sfx_locater?sid=ALEPH:EBI01&amp;genre=book&amp;isbn=9783319415406</subfield>
      <subfield code="z">Online via SFX</subfield>
    </datafield>
    <datafield ind1="4" ind2=" " tag="856">
      <subfield code="u">http://dx.doi.org/10.1007/978-3-319-41540-6</subfield>
      <subfield code="x">Verlag</subfield>
    </datafield>
  </record>
</recordData>
</record>
<record>
<recordSchema>marcxml</recordSchema>
<recordPacking>xml</recordPacking>
<recordData>
<record xmlns="http://www.loc.gov/MARC21/slim">
    <leader>00000cam a2200000Mi 4500</leader>
    <controlfield tag="001">932169433</controlfield>
    <controlfield tag="008">151012t20152015sz a    ob    101 0 eng d</controlfield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="a">9783319245409</subfield>
      <subfield code="q">electronic bk.</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="a">3319245406</subfield>
      <subfield code="q">electronic bk.</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="z">9783319245393</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="z">3319245392 (print)</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="020">
      <subfield code="z">9783319245393 (print)</subfield>
    </datafield>
    <datafield ind1="1" ind2="0" tag="245">
      <subfield code="a">Cognitive Radio Oriented Wireless Networks :</subfield>
      <subfield code="b">10th International Conference, CROWNCOM 2015, Doha, Qatar, April 21-23, 2015, revised selected papers /</subfield>
      <subfield code="c">edited by Mark Weichold, Mounir Hamdi, Muhammad Zeeshan Shakir, Mohamed Abdallah, George K. Karagiannidis, Muhammad Ismail.</subfield>
    </datafield>
    <datafield ind1=" " ind2="1" tag="264">
      <subfield code="a">Cham :</subfield>
      <subfield code="b">Springer,</subfield>
      <subfield code="c">2015.</subfield>
    </datafield>
    <datafield ind1=" " ind2="4" tag="264">
      <subfield code="c">©2015</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="300">
      <subfield code="a">1 online resource (xv, 805 pages) :</subfield>
      <subfield code="b">illustrations.</subfield>
    </datafield>
    <datafield ind1="1" ind2=" " tag="490">
      <subfield code="a">Lecture notes of the Institute for Computer Sciences, Social Informatics and Telecommunications Engineering,</subfield>
      <subfield code="x">1867-8211 ;</subfield>
      <subfield code="v">156</subfield>
    </datafield>
    <datafield ind1="0" ind2=" " tag="505">
      <subfield code="a">Dynamic Spectrum Access/Management -- Networking Protocols for CR -- Modeling and Theory -- HW Architecture and Implementations -- Next Generation of Cognitive Networks -- Standards and Business Models -- Emerging Applications for Cognitive Networks.</subfield>
    </datafield>
    <datafield ind1=" " ind2=" " tag="520">
      <subfield code="a">This book constitutes the thoroughly refereed post-conference proceedings of the 10th International Conference on Cognitive Radio Oriented Wireless Networks, CROWNCOM 2015, held in Doha, Qatar, in April 2015. The 66 revised full papers presented were carefully reviewed and selected from 110 submissions and cover the evolution of cognitive radio technology pertaining to 5G networks. The papers are clustered to topics on dynamic spectrum access/management, networking protocols for CR, modeling and theory, HW architecture and implementations, next generation of cognitive networks, standards and business models, and emerging applications for cognitive networks.</subfield>
    </datafield>
    <datafield ind1=" " ind2="0" tag="650">
      <subfield code="a">Cognitive radio networks</subfield>
      <subfield code="v">Congresses.</subfield>
    </datafield>
  </record>
</recordData>
</record>
<nextRecordPosition>201</nextRecordPosition>
<resultSetIdleTime/>
<echoedSearchRetrieveRequest xmlns:srw="http://www.loc.gov/zing/srw/">
<version>1.1</version>
<query>srw.su="Computer organization"</query>
<maximumRecords>100</maximumRecords>
<recordPacking>xml</recordPacking>
<startRecord>101</startRecord>
<sortKeys>relevance</sortKeys>
<wskey>7Rn7E7osoeJeQURAiEO4GH74HZa6BLdt7eXahgxdvwnfO6Ph7za1OzU9M2zx0e9nuDHVO34b5HfnLuOw</wskey>
</echoedSearchRetrieveRequest>
</searchRetrieveResponse>

I have a feeling part of the problem comes from that there are <record> elements in two namespaces. I am trying to retrieve the one in the " http://www.loc.gov/MARC21/slim " namespace.

How do I solve the problem?

You didn't use the registerd namespace prefix ns in your XPath. Without using that prefix, it is assumed that you want to find record elements in empty namespace, hence no result was found ( $xpatheval->length value was 0) :

$responseXPath->registerNamespace("ns", "http://www.loc.gov/MARC21/slim");
$xpatheval = $responseXPath->query('//ns:record');
echo $xpatheval->length;

demo

output :

2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM