您能否在不使用 XSL 转换/解析 XML 的情况下在 XML 中声明和使用变量 [英] Can you declare and use a variable inside an XML without using XSL to transform/parse the XML

查看:25
本文介绍了您能否在不使用 XSL 转换/解析 XML 的情况下在 XML 中声明和使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个要在应用程序中读取的 XML 元素,但是您有多个环境,依赖文件的路径可能会发生变化

Say you have an XML element you want to read in an app, however you have multiple environments where the path of dependent files may change

<root>
  <element ID="MyConfigFile" url="c:\Program Files\MyProgram\resources\MyProgramconfig.xml" />
  <element ID="Executable" url="c:\Program Files\MyProgram\Prog.exe" />
</root>

...所以你想引用相对目录

...so you would want to make a reference to the relative directory

@path="c:\Program Files\MyProgram\"

@path="c:\Program Files\MyProgram\"

<root>
  <element ID="MyConfigFile" url="@path\resources\MyProgramconfig.xml" />
  <element ID="Executable" url="@path\Prog.exe" />
</root>

是否可以使用在 XML 本身中声明的变量来引用相对目录路径?

Can you use a variable declared in the XML itself to reference the relative directory path?

推荐答案

您需要在文件中添加 DOCTYPE 声明,在那里声明一个实体,然后在文档正文中引用该实体.

You would need to add a DOCTYPE declaration to your file, declare an entity there and then reference that entity in the document body.

<!DOCTYPE root [
  <!ENTITY path "c:\Program Files\MyProgram">
]>
<root>
  <element ID="MyConfigFile" url="&path;\resources\MyProgramconfig.xml" />
  <element ID="Executable" url="&path;\Prog.exe" />
</root>

这篇关于您能否在不使用 XSL 转换/解析 XML 的情况下在 XML 中声明和使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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