#!/usr/local/bin/perl

use strict;
use warnings;

use Sleepycat::DbXml 'simple' ;

#
# Example 3
#
# Create an XML Container, add a document that includes a
# namespace definition, create a query context, define a
# namespace prefix to URI mapping, query the container
# for the document within a context, iterate over the
# result set displaying the values returned.
#

eval
{
	my $container = new XmlContainer("test.dbxml");
	$container->open(Db::DB_CREATE);
	my $document = new XmlDocument ;
	my $content = <<EOM;
	<book xmlns:books1='http://foo.bar.com/books.dtd'>
	    <books1:title>Knowledge Discovery in Databases.</books1:title>
	</book>
EOM
	$document->setContent($content);
	$container->putDocument($document);
	my $context = new XmlQueryContext ;
	$context->setNamespace("books2","http://foo.bar.com/books.dtd");
	my $results = $container->queryWithXPath("/*[books2:title='Knowledge Discovery in Databases.']",$context);
	my $value = new XmlValue ;
	while($results->next($value))
	{
		my $document = $value->asDocument();
		print $document->getID() . " = " . $value->asString() . "\n";
	}
	$container->close();
	$container->remove();
};


if (my $e = catch std::exception)
{
	warn $e->what() . "\n";
}
elsif ($@)
{
        warn $@;
}
