Question

I am reading a XML file and want to replace the XPath in "print" as "@href="*?releaseStatus=RELEASED" so that I will make it generic to get the first occurrence of the instance. How can I achieve that?

use XML::LibXML;

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file("type_book.xml");

for my $chapter_node ($doc->findnodes('/book/contents'))
{
print $chapter_node->findvalue('locator[@href ="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED"]/@title');
}

XML File:

<book>
<contents>
<locator href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?  releaseStatus=RELEASED" role="http://www.abc.org/roles/book-part-locator" title="Photonic crystal light-emitting sources">
</locator>
<locator href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&amp;format=pdf" role="http://www.abc.org/roles/book-part-pdf-locator" title="Photonic crystal light-emitting sources">
</locator>
<locator href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&amp;format=epub" role="http://www.abc.org/roles/book-part-epub-locator" title="Photonic crystal light-emitting sources">
</locator>
</contents>
</book>
Was it helpful?

Solution

Good enough?

locator[contains(@href, "?releaseStatus=RELEASED")]/@title

Using XPath 2 functions (Not sure if supported by XML::LibXML)

locator[fn:matches(@href, "^.*(\?|&)releaseStatus=RELEASED(&|$)")]/@title

You might need to associate fn with http://www.w3.org/2005/xpath-functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top