我想使用.opf-file解析SimpleXML的内容(这是一个包含电子书文件元数据的文件-.epubs,在我的例子中)。
但是,我没有成功获得像<dc:title><dc:creator opf:file-as="xxx" opf:role="xxx">这样的项目。
对于hitchhiker.opf文件(见下文),我尝试了以下操作:

<?php
$opf = simplexml_load_file("hitchhiker.opf");
$namespaces = $opf->getNameSpaces(true);
$dc = $opf->children($namespaces['dc']);
echo $dc->title;
echo "\r\n";
echo $dc->creator;
?>

但这不会产生产出。
如果使用其他工具比simplexml更容易做到这一点,那么这也可以。
搭便车者.opf:
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="uuid_id">
    <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
        <dc:identifier opf:scheme="calibre" id="calibre_id">8</dc:identifier>
        <dc:identifier opf:scheme="uuid" id="uuid_id">36515eb7-1a25-4469-b55e-0042d45ca508</dc:identifier>
        <dc:title>The Hitchhiker's Guide to the Galaxy</dc:title>
        <dc:creator opf:file-as="Adams, Douglas" opf:role="aut">Douglas Adams</dc:creator>
        <dc:contributor opf:file-as="calibre" opf:role="bkp">calibre (0.7.43) [http://calibre-ebook.com]</dc:contributor>
        <dc:date>1997-06-22T07:00:00+00:00</dc:date>
        <dc:description>SUMMARY:
Don't panic! You're not timetripping! It's the tenth anniversary of the publication of Douglas Adams's zany, best-selling novel, and to celebrate Harmony is reissuing a special edition of this cult classic!By now the story is legendary. Arthur Dent, mild-mannered, out-to-lunch earth-ling, is plucked from his planet by his friend Ford Prefect just seconds before it was demolished to make way for a hyper-space bypass. Ford, posing as an out-of-work actor, is a researcher for the revised edition of The Hitchhiker's Guide to the Galaxy. Together the gruesome twosome begin their now-famous inter-galactic journey through time, space and best-sellerdom.For Hitchhiker fanatics (you know who you are!) who've read the books, seen the television program, and listened to the radio show, as well as newcomers to Douglas Adams's unique universe -- remember -- don't panic, don't forget to bring a towel, and don't forget to celebrate The Hitchhiker's Guide to the Galaxy's tenth anniversary by wearing your bathrobe.From the Hardcover edition.</dc:description>
        <dc:publisher>Random House, Inc.</dc:publisher>
        <dc:identifier opf:scheme="ISBN">9780345418913</dc:identifier>
        <dc:language>en</dc:language>
        <dc:subject>Science-Fiction</dc:subject>
    </metadata>
    <guide>
        <reference href="Hitchhiker's Guide to the Galaxy, The - Douglas Adams.jpg" type="cover" title="Cover"/>
    </guide>
</package>

最佳答案

您忘记了元数据元素,例如。

$package = simplexml_load_file('hitchhiker.opf');
echo $package->metadata->children('dc', true)->title;

输出:银河系漫游指南

10-04 16:57