#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
use XML::LibXSLT;
use open ':std',':locale';

my $tmpdoc=XML::LibXML->createDocument();
{my $el=$tmpdoc->createElement('doc');
$tmpdoc->setDocumentElement($el);
my $e1=$tmpdoc->createElement('test');
$e1->appendTextNode('blah balh');
$el->appendChild($e1);
}
my $doc_mem=XML::LibXML->createDocument();
{my $el=$doc_mem->createElement('test');
$doc_mem->setDocumentElement($el);
my $e1=$doc_mem->createElement('some');
$el->appendChild($e1);
}

my $NS='urn:test';
{
my $p=XML::LibXML->new();
my $t=XML::LibXSLT->new();

$t->register_function($NS,'doc',\&test_doc);
$t->register_function($NS,'str',\&test_str);
$t->debug_callback(sub{print@_});

my $doc=$p->parse_file('test.xml');
my $st=$t->parse_stylesheet_file('test.xsl');

my $out=$st->transform($doc);
print $st->output_as_chars($out);
$out=$st->transform($doc_mem);
print $st->output_as_chars($out);
}

sub test_doc {
    my $odoc=XML::LibXML::Document->new();
    my $el=$odoc->createElementNS($NS,'doc');
    $odoc->setDocumentElement($el);
    $el->appendTextNode('a test');
    $el->appendChild($odoc->importNode(
        ($tmpdoc->findnodes('/doc/test'))[0]));
    $el->appendChild($odoc->importNode(
        ($doc_mem->findnodes('/test'))[0]));
    warn('returning from test_doc');
    return $odoc;
}

sub test_str {
    return 'a string';
}
