开发者

How do I use an environment variables in xi:include of a XML file?

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.


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">


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜