如何通过使用php获取xml节点的xlink:href属性的值

时间:2010-08-22 15:58:15

标签: php xml xlink

我不能这样做,不要知道乳清。如何通过使用php获取xml节点的xlink:href属性的值。请有人给我一个轻推。我是php的新手

这是XML文档

<?xml version="1.0" encoding="UTF-8"?>
<topicMap id="1HLCM3FXT-28MTV0W-50"
    xmlns="http://www.topicmaps.org/xtm/1.0/" xmlns:xlink="http://www.w3.org/1999/xlink">
    <topic id="1HLCM7CDQ-21WQN9G-66">
        <instanceOf>
            <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#concept"/>
        </instanceOf>
        <baseName>
            <baseNameString><![CDATA[feathers]]></baseNameString>
        </baseName>
        <occurrence>
            <resourceRef xlink:type="simple" xlink:href="file:/./Birds_concept - about birds/feathers.txt"/>
        </occurrence>
    </topic>
</topicMap>

1 个答案:

答案 0 :(得分:2)

使用the DOMone of the *NS functions, like getAttributeNS

$doc = new DOMDocument();
$doc->loadXML($your_xml_string);
$resource_refs = $doc->getElementsByTagName('resourceRef');
foreach($resource_refs as $rr)
    print_r( $rr->getAttributeNS('http://www.w3.org/1999/xlink', 'href') );

(这是未经测试的代码; print_r可能无法按预期工作.getAttributeNS返回node list,节点列表中的每个项目都是an attribute。getAttributeNS页面上的文档有另一个例子。 )