如何在 XML 文件的 xi:include 中使用环境变量?

发布于 2024-08-17 18:45:12 字数 303 浏览 8 评论 0原文

我想在 XML 文件的 xi:include 节点的 href 链接中使用环境变量。目前 Perl XInclude 解析器不支持任何变量,只支持绝对路径。有没有一种方法可以克服这个问题? 例如,我的 xi:include 节点将如下所示:

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

稍后可以通过加载不同的设置来更改由 $GLOBAL_ROOT 引用的路径,并且解析器仍然应该能够在相应的位置查找该文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生再见 2024-08-24 18:45:12

为什么不使用符号链接(相对或绝对)?或用户主目录位置(~/.xml)?

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

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

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

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">
清风疏影 2024-08-24 18:45:12

您在专门使用私有方法(按照惯例用前导下划线表示)的基础上不稳定,但作者可以通过提供更多钩子来提供帮助。假设您正在使用 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);
}

然后向工厂传递一个 MyXInclude::ExpandEnvironmentVars 实例作为 Handler 参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文