+sub find_or_make_node {
+ my($xc, $path, $recursion_level) = @_;
+
+ die "deep recursion in find_or_make_node($path)"
+ if $recursion_level == 10;
+ $path = "." if $path eq "";
+
+ my @nodes = $xc->findnodes($path);
+ if (@nodes == 0) {
+ # Oh dear, the parent node doesn't exist. We could make it,
+ my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
+ $ppath = "" if !defined $ppath;
+ #warn "path='$path', ppath='$ppath', element='$element'";
+ #warn "no node '$path': making it";
+ my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
+
+ my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
+ #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
+ my $new = new XML::LibXML::Element($nsElem);
+ if (defined $prefix) {
+ #warn "setNamespace($prefix)";
+ $new->setNamespace(irspy_namespace($prefix), $prefix);
+ }
+
+ $parent->appendChild($new);
+ return $new;
+ }
+ warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
+ return $nodes[0];
+}
+
+