我有XML:
http://localhost:8888/?purp=oclcn&xml=<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<record xmlns="http://www.loc.gov/MARC21/slim">
<leader>00000cam a2200000 a 4500</leader>
<controlfield tag="001">33333502</controlfield>
<controlfield tag="008">951010s1996 vtua b 001 0 eng </controlfield>
<datafield ind1=" " ind2=" " tag="010">
<subfield code="a"> 95045582 </subfield>
</datafield>
<datafield ind1=" " ind2=" " tag="020">
<subfield code="a">1858983274</subfield>
</datafield>
<datafield ind1=" " ind2=" " tag="020">
<subfield code="a">9781858983271</subfield>
</datafield>
<datafield ind1="0" ind2="0" tag="245">
<subfield code="a">Economic sociology /</subfield>
<subfield code="c">edited by Richard Swedberg.</subfield>
</datafield>
<datafield ind1=" " ind2=" " tag="260">
<subfield code="a">Cheltenham, Glos, UK ;</subfield>
<subfield code="a">Brookfield, Vt., US :</subfield>
<subfield code="b">E. Elgar Pub. Co.,</subfield>
<subfield code="c">©1996.</subfield>
</datafield>
<datafield ind1=" " ind2=" " tag="300">
<subfield code="a">xv, 574 pages :</subfield>
<subfield code="b">illustrations ;</subfield>
<subfield code="c">25 cm.</subfield>
</datafield>
<datafield ind1="1" ind2=" " tag="490">
<subfield code="a">The international library of critical writings in sociology ;</subfield>
<subfield code="v">5</subfield>
</datafield>
<datafield ind1="1" ind2=" " tag="490">
<subfield code="a">An Elgar reference collection</subfield>
</datafield>
<datafield ind1=" " ind2=" " tag="500">
<subfield code="a">A collection of journal articles previously published between 1940-1994.</subfield>
</datafield>
<datafield ind1=" " ind2="0" tag="650">
<subfield code="a">Economics</subfield>
<subfield code="x">Sociological aspects.</subfield>
</datafield>
<datafield ind1=" " ind2="0" tag="650">
<subfield code="a">Sociology.</subfield>
</datafield>
<datafield ind1=" " ind2="0" tag="650">
<subfield code="a">Economics.</subfield>
</datafield>
<datafield ind1=" " ind2="6" tag="650">
<subfield code="a">Économie politique</subfield>
<subfield code="x">Aspect sociologique.</subfield>
</datafield>
<datafield ind1=" " ind2="6" tag="650">
<subfield code="a">Sociologie.</subfield>
</datafield>
<datafield ind1=" " ind2="6" tag="650">
<subfield code="a">Économie politique.</subfield>
</datafield>
<datafield ind1=" " ind2="7" tag="650">
<subfield code="a">Economics.</subfield>
<subfield code="2">fast</subfield>
<subfield code="0">(OCoLC)fst00902116</subfield>
</datafield>
<datafield ind1=" " ind2="7" tag="650">
<subfield code="a">Economics</subfield>
<subfield code="x">Sociological aspects.</subfield>
<subfield code="2">fast</subfield>
<subfield code="0">(OCoLC)fst00902213</subfield>
</datafield>
<datafield ind1=" " ind2="7" tag="650">
<subfield code="a">Sociology.</subfield>
<subfield code="2">fast</subfield>
<subfield code="0">(OCoLC)fst01123875</subfield>
</datafield>
<datafield ind1="1" ind2="7" tag="650">
<subfield code="a">Economische sociologie.</subfield>
<subfield code="2">gtt</subfield>
</datafield>
<datafield ind1=" " ind2="7" tag="650">
<subfield code="a">Sociologie économique.</subfield>
<subfield code="2">ram</subfield>
</datafield>
<datafield ind1=" " ind2="7" tag="650">
<subfield code="a">Économie politique</subfield>
<subfield code="x">Sociologie.</subfield>
<subfield code="2">ram</subfield>
</datafield>
<datafield ind1="0" ind2="7" tag="650">
<subfield code="a">Wirtschaftssoziologie.</subfield>
<subfield code="2">swd</subfield>
</datafield>
<datafield ind1=" " ind2="4" tag="650">
<subfield code="a">Sociologie.</subfield>
</datafield>
<datafield ind1=" " ind2="4" tag="650">
<subfield code="a">Économie politique.</subfield>
</datafield>
<datafield ind1=" " ind2="4" tag="650">
<subfield code="a">Économie politique - Aspect sociologique.</subfield>
</datafield>
<datafield ind1="0" ind2="7" tag="650">
<subfield code="a">Wirtschaftssoziologie.</subfield>
<subfield code="0">(DE-588)4066514-8</subfield>
<subfield code="2">gnd</subfield>
</datafield>
<datafield ind1="1" ind2=" " tag="700">
<subfield code="a">Swedberg, Richard.</subfield>
</datafield>
</record>
我希望检索属性为tag = 001的元素中的文本。我使用了以下代码:
$xml = new DomDocument;
$xmlstring = $_GET['xml'];
$new = $xml->loadXML($xmlstring);
$xpath = new DomXpath($xml);
var_dump($new, $xpath);
$h = $xpath->query('//controlfield[@tag="001"]');
var_dump ($h);
转储的$h
中的DOMNodeList长度为0。怎么会?它不应该有一个元素吗?
答案 0 :(得分:1)
查询字符串的格式应为//{root node}/{child node}/..
此处的另一个问题是根节点包含默认命名空间xlmns="http://www.loc.gov/MARC21/slim"
,但DomXPath::query
仅读取没有任何命名空间的根节点。
因此,我们必须首先注册此名称空间。代码应如下所示:
$xml = new DomDocument;
$xmlstring = $_GET['xml'];
$new = $xml->loadXML($xmlstring);
$xpath = new DomXpath($xml);
$xpath->registerNamespace("ns", "http://www.loc.gov/MARC21/slim");
var_dump($new, $xpath);
$h = $xpath->query('//ns:record/ns:controlfield[@tag="001"]/..');
var_dump ($h);