#!/usr/local/bin/perl

use strict;
use warnings;

use Sleepycat::DbXml 'simple' ;

#
# Example 5
#
# Create an XML Container, define an equality string index
# for booktitle elements, add a document, create a query
# context, define a variable binding, query the container
# for the document within a context referencing the variable
# defined, iterate over the result set displaying the values
# returned.
#
# Note that this example assumes that the 'test' container
# does not already exist. If it does exist then the
# declareIndex method will throw an exception to complain
# that the container isn't empty.
#

eval
{
	my $container = new XmlContainer("test.dbxml");
	$container->open(Db::DB_CREATE);
	$container->addIndex("","title","node-element-equality-string");
	my $document = new XmlDocument ;
	my $content = "<book><title>Knowledge Discovery in Databases.</title></book>";
	$document->setContent($content);
	$container->putDocument($document);
	my $context = new XmlQueryContext ;
	$context->setVariableValue("title","Knowledge Discovery in Databases.");
	my $results = $container->queryWithXPath('//*[title=$title]',$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 $@ ;
}
