Robie Nayak
Robie Nayak

Reputation: 827

Not able to get value of element in XPath context in perl

I am not able to get value for chapter_title, though it gets values for other attributes. I have shown the structure of the 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";
}
}

The structure of the 'book.xml' file is as below:

<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>

I wonder what causes it fail to get the value. Any help would be appreciable. Sorry, some part of xml was missing.

<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>

And the exact error I get is

XPath error : Undefined namespace prefix error : xmlXPathCompOpEval: parameter error error : xmlXPathCompiledEval: 1 objects left on the stack.

Upvotes: 3

Views: 720

Answers (1)

ikegami
ikegami

Reputation: 386371

You didn't use the XPC, so it doesn't know waht xlink means in your path.

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

should be

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

Upvotes: 3

Related Questions