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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
joe
  • 21
  • 3

2 Answers2

2

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
  • 4,583
  • 1
  • 26
  • 33
1

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
  • 134,834
  • 32
  • 188
  • 245