-# $Id: Utils.pm,v 1.8 2006-11-08 17:41:27 mike Exp $
+# $Id: Utils.pm,v 1.9 2006-11-09 16:09:35 mike Exp $
package ZOOM::IRSpy::Utils;
}
+# PRIVATE to irspy_namespace() and irspy_xpath_context()
+my %_namespaces = (
+ e => 'http://explain.z3950.org/dtd/2.0/',
+ i => $IRSPY_NS,
+ );
+
+
+sub irspy_namespace {
+ my($prefix) = @_;
+
+ my $uri = $_namespaces{$prefix};
+ die "irspy_namespace(): no URI for namespace prefix '$prefix'"
+ if !defined $uri;
+
+ return $uri;
+}
+
+
sub irspy_xpath_context {
my($record) = @_;
my $doc = $parser->parse_string($xml);
my $root = $doc->getDocumentElement();
my $xc = XML::LibXML::XPathContext->new($root);
- $xc->registerNs(e => 'http://explain.z3950.org/dtd/2.0/');
- $xc->registerNs(i => $IRSPY_NS);
+ foreach my $prefix (keys %_namespaces) {
+ $xc->registerNs($prefix, $_namespaces{$prefix});
+ }
return $xc;
}
if ($value ne $node->getValue()) {
$node->setValue($value);
$nchanges++;
- print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
+ #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
}
} elsif ($node->isa("XML::LibXML::Element")) {
# The contents could be any mixture of text and
my $child = new XML::LibXML::Text($value);
$node->appendChild($child);
$nchanges++;
- print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
+ #print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
} else {
warn "unexpected node type $node";
}
next if !$value; # No need to create a new empty node
my($ppath, $element) = $xpath =~ /(.*)\/(.*)/;
dom_add_element($xc, $ppath, $element, $value, @addAfter);
- print "New $key ($xpath) = '$value'<br/>\n";
+ #print "New $key ($xpath) = '$value'<br/>\n";
$nchanges++;
}
}
sub dom_add_element {
my($xc, $ppath, $element, $value, @addAfter) = @_;
- print "Adding $element='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
+ #print "Adding $element='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
my @nodes = $xc->findnodes($ppath);
if (@nodes == 0) {
# Oh dear, the parent node doesn't exist. We could make it,
warn scalar(@nodes), " nodes match parent '$ppath'" if @nodes > 1;
my $node = $nodes[0];
- my $new = new XML::LibXML::Element($element);
+ my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
+ my $new = new XML::LibXML::Element($nsElem);
+ $new->setNamespace(irspy_namespace($prefix), $prefix)
+ if $prefix ne "";
+
$new->appendText($value);
foreach my $predecessor (reverse @addAfter) {
my($child) = $xc->findnodes($predecessor, $node);
if (defined $child) {
$node->insertAfter($new, $child);
- print "Added after '$predecessor'<br/>\n";
+ #print "Added after '$predecessor'<br/>\n";
return;
}
}
my @children = $node->childNodes();
if (@children) {
$node->insertBefore($new, $children[0]);
- print "Added new first child<br/>\n";
+ #print "Added new first child<br/>\n";
} else {
$node->appendChild($new);
- print "Added new only child<br/>\n";
+ #print "Added new only child<br/>\n";
}
if (0) {