使用 Saxon 读取 XSLT 样式表中的环境变量 [英] Reading Environment Variables in an XSLT Stylesheet with Saxon

查看:41
本文介绍了使用 Saxon 读取 XSLT 样式表中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在某个任意元素或属性中使用我机器的主机名生成一个 XML 文件,例如

I'm trying to generate an XML file with the my machine's hostname in some arbitrary element or attribute, e.g.

<hostname>myHostname</hostname>

我使用的是 Saxon 9.2.我可以想到三种方法来做到这一点:

I'm using Saxon 9.2. I can think of three ways to do this:

  1. 读取并解析/etc/sysconfig/network(我使用的是 Fedora)
  2. 读取环境变量(如$ echo $HOSTNAME)
  3. 将主机名传递给 saxon,然后以某种方式取消引用变量(不确定这是否可行)
  1. Read and parse /etc/sysconfig/network (I'm using Fedora)
  2. Read the environment variable (as in $ echo $HOSTNAME)
  3. Pass the hostname to saxon and then use somehow dereference a variable (not sure if this is possible)

这些有可能吗?我认为第一个选项最有可能起作用,但我认为其他两个选项会产生不那么冗长的 XSLT.

Are any of these possible? I think the first option is most likely to work, but I think the other two options will produce less verbose XSLT.

我也有一个相关的问题:

I also have a related question:

目前,我有一个 XSLT 和源 XML 文件,可生成一堆 XML 文件,它的工作原理与我预期的一样.无论如何,我可以有选择地为每个主机生成一个文件吗?也就是说,我想说'如果主机名是 myHostName 则为 myHostName 生成 XML 文件,如果主机名是 myOtherHostName 则为 myOtherHostName 生成 XML 文件'.

Currently, I have an XSLT and source XML file that generates a bunch of XML files, it works like I expect it to. Is there anyway I can selectively generate one file per host? That is, I want to say 'if the hostname is myHostName then generate the XML file for myHostName, if the hostname is myOtherHostName then generate the XML file for myOtherHostName'.

我问这个是因为我正在尝试配置大量机器,如果我可以在每台机器上放置一个 XSLT 和 XML 文件,然后在每台机器上调用相同的命令,然后在每台机器上获得正确的 XML真的很方便.

I ask this because I'm trying to configure a large number of machines and if I could drop an XSLT and XML file on each and then call the same command on every machine and hten get the right XML on each it would be really convienent.

推荐答案

您应该在调用" xslt 时将参数传递给它.我认为这是最强大的解决方案.

You should pass a parameter to your xslt when "calling" it. I think this is the most robust solution.

所以在你的样式表的顶部,你会有类似的东西:

So at the top of your stylesheet you would have something like :

<xsl:param name="hostName"/>

然后您可以通过通常的符号在您的 .xslt 中使用它:$hostName

Then you can use it in your .xslt via the usual notation : $hostName etc.

您只需在调用 xslt 处理器时传递这些参数.根据您的使用方式,这可能会有所不同.

You just then need to pass those parameters when calling the xslt processor. Depending on how you use it this may vary.

这篇关于使用 Saxon 读取 XSLT 样式表中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆