3
雖然它獲取其他屬性的值,但我無法獲取chapter_title的值。我已經展示了xml的結構。無法獲取perl中XPath上下文中元素的值
use XML::LibXML;
my %chapter_columns =
(
'Book_Id' => 'substring-before(@id,"ch")',
'Chapter_Doi' => 'book:meta/@doi',
'Chapter_Id' => '@id',
'Chapter_Title' => 'book:locator[contains(@xlink:href, "format=epub")]/@xlink:title',
'Chapter_Doi_Prefix' => 'substring-before(book:meta/@doi,"/")',
);
my $parser = XML::LibXML->new();
my $dom = $parser->parse_file("book.xml");
my $root = $dom->documentElement();
my $xpc = XML::LibXML::XPathContext->new($root);
$xpc->registerNs('book', 'http://api.abc.org/Book/1.0/');
$xpc->registerNs('xlink', 'http://www.w3.org/1999/xlink');
foreach my $chapter_node ($xpc->findnodes('/book:bookResource/book:book/
book:contents/book:chapter'))
{
foreach my $col(qw/Chapter_Id Chapter_Title Book_Id Chapter_Doi Chapter_Doi_Prefix
Chapter_Id/)
{
print $chapter_node->findvalue($chapter_columns{$col}), "\n";
}
}
的「是book.xml」文件的結構如下:
<book:bookResource xmlns:book="http://api.abc.org/Book/1.0/"
xmlns:xlink="http://www.w3.org/1999/xlink">
<book:book>
<book:contents>
<book:chapter id="bk111111ch3" type="CHAPTER">
<book:locator xlink:href="/book/isbn/979-0-1234-1111-1/book-part/chapter/bk111111ch3?
releaseStatus=UNRELEASED" xlink:title="Statics" xlink:type="locator"
xmlns:xlink="http://www.w3.org/1999/xlink"/>
<book:locator xlink:href="/book/isbn/979-0-1234-1111-1/book-part/chapter/bk111111ch3?
releaseStatus=UNRELEASED&format=pdf" xlink:title="Statics" xlink:type="locator"
xmlns:xlink="http://www.w3.org/1999/xlink"/>
<book:locator xlink:href="/book/isbn/979-0-1234-1111-1/book-part/chapter/bk111111ch3?
releaseStatus=UNRELEASED&format=epub" xlink:title="Statics" xlink:type="locator"
xmlns:xlink="http://www.w3.org/1999/xlink"/>
<book:meta doi="10.1088/bk111111ch3" firstPage="3-1" lastPage="3-22"></book:meta>
</book:chapter>
</book:contents>
</book:book>
</book:bookResource>
我不知道是什麼原因導致其無法獲得的價值。任何幫助將是可觀的。對不起,xml的某些部分丟失了。
<book:bookResource xmlns:book="http://api.abc.org/Book/1.0/">
<book:book>
<book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/book/isbn/979-0-
4444-1000-17?releaseStatus=RELEASED" xlink:title="979-0-4444-1000-17"
xlink:type="locator">
</book:locator>
<book:contents>
<book:chapter id="bk444444ch1" type="CHAPTER">
<book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/book/isbn/979-0-
4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED"
xlink:role="http://www.abc.org/roles/book-part-locator" xlink:title="Photonic" xlink:type="locator"></book:locator>
<book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/book/isbn/979-0-
4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&format=pdf"
xlink:role="http://www.abc.org/roles/book-part-pdf-locator" xlink:title="Photonic"
xlink:type="locator"></book:locator>
<book:locator xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/book/isbn/979-0-
4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&format=epub"
xlink:role="http://www.abc.org/roles/book-part-epub-locator" xlink:title="Photonic"
xlink:type="locator"></book:locator>
<book:meta doi="10.1088/bk444444ch1" firstPage="1-1" lastPage="1-118">
<book:author givenName="J E" surname="Field">
<book:affiliation xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="bk444444ch1af1"
xlink:role="http://www.abc.org/roles/affiliation-locator" xlink:type="locator">
</book:affiliation>
</book:author>
</book:meta>
</book:chapter>
</book:contents>
</book:book>
</book:bookResource>
以及確切的錯誤我得到的是
的XPath錯誤:未定義命名空間前綴 錯誤:xmlXPathCompOpEval:參數錯誤 錯誤:xmlXPathCompiledEval:1對象留在堆棧中。
爲我工作。您的腳本將爲Chapter_Title xpath返回「單電荷載體離聚物電致反應的靜力學和動力學」。 –
適合我。該程序打印標題「單電荷載體離聚物電致動靜力學和動力學」。 - 可能會將[libxml升級到2.9](http://xmlsoft.org/downloads.html)和[XML-LibXML升級到2](https://metacpan.org/release/XML-LibXML)。 – daxim
對不起,我錯過了xml的一部分: – Robie