无法在perl中获取XPath上下文中元素的值

时间:2013-07-18 17:17:59

标签: perl xpath

虽然它获取了其他属性的值,但我无法获得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个对象。

1 个答案:

答案 0 :(得分:3)

您没有使用XPC,因此它不知道xlink在您的路径中意味着什么。

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

应该是

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