5
我有這樣一個XML的工作:(這是EPUB圖書中標準container.xml)PHP的DOMXPath中需要registerNamespace嗎?
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/9780765348210.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
我想用PHP解析它。這是到目前爲止我的代碼:
$c = new DOMDocument();
$c->load($filename);
$x = new DOMXPath($c);
//fine up to here!
//is this even what I'm supposed to be doing?
$x->registerNamespace('epub', 'urn:oasis:names:tc:opendocument:xmlns:container');
$root = $x->query('/epub:container/epub:rootfiles/epub:rootfile');
//fine down from here!
$opf = $root->item(0)->getAttribute('full-path'); //I know I should check if the element's there and if it has the attribute. Not important.
我的問題是:有沒有辦法不這樣做registerNamespace
電話嗎?我不知道如果不同的epubs設置這個值有點不同,我需要這個代碼來處理任何epub我扔它。