PHP DOMXPath中的Xpath不起作用

时间:2017-01-15 01:57:35

标签: php xpath

我无法使用以下DOMX路径:

    $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()是我使用的类似于var_dump的自定义库函数。当我转换$ xpatheval-> length时,我得到0,当它应该大于0时,因为$ xpatheval应该包含相关的XML元素。

这是$ 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>

我感觉问题的一部分来自于两个名称空间中有<record>个元素。我正在尝试检索“http://www.loc.gov/MARC21/slim”命名空间中的那个。

如何解决问题?

1 个答案:

答案 0 :(得分:0)

您没有在XPath中使用registerd命名空间前缀ns。如果不使用该前缀,则假定您要在空命名空间中查找record个元素,因此未找到任何结果($xpatheval->length值为0):

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

<强> demo

输出

2