3

我无法获得 chapter_title 的值,尽管它获得了其他属性的值。我已经展示了xml的结构。

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&amp;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&amp;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&amp;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&amp;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 个对象留在堆栈上。

4

1 回答 1

3

您没有使用 XPC,因此它不知道xlink您的路径中的含义。

print $chapter_node->findvalue($chapter_columns{$col}), "\n";

应该

print $xpc->findvalue($chapter_columns{$col}, $chapter_node), "\n";
于 2013-07-19T02:12:26.517 回答