views:

239

answers:

2

I would like to use an environment variable in the href link of the xi:include node of an XML file. Currently the Perl XInclude parser doesn't support any variables but only absolute paths. Is there a method where this can be overcome? for example, my xi:include node will look like this:

<xi:include href="$GLOBAL_ROOT/alternative.xml">

The path referred by $GLOBAL_ROOT can be changed later by loading a different setting and still the parser should be able to look up in the respective location for the file.

+1  A: 

You're on shaky ground specializing a private method (indicated by convention with a leading underscore), but the author could help by providing more hooks. Assuming you're using XML::Filter::XInclude:

package MyXInclude::ExpandEnvironmentVars;

use warnings;
use strict;

our @ISA = qw/ XML::Filter::XInclude /;

sub _include_xml_document {
  my($self,$url) = @_;

  # expand environment variables if present
  $url =~ s/\$(\w+)/$ENV{$1}/g;

  $self->SUPER::_include_xml_document($url);
}

Then pass the factory an instance of MyXInclude::ExpandEnvironmentVars as the Handler parameter.

Greg Bacon
+1  A: 

Why not use a symbolic link (either relative or absolute)? or a user home dir location (~/.xml)?

<xi:include href="XML/alternative.xml">

<xi:include href="/XML/alternative.xml">

<xi:include href="~/.XML/alternative.xml">
Don