xml xsl:import 或 xsl:include 的相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/142058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:06:03  来源:igfitidea点击:

Relative path for xsl:import or xsl:include

xmlxsltvbscriptclient-side

提问by alumb

I am trying to use VBScript to do an XSLT transform on an XML object.
The XSL file I'm translating includes the <xsl:import href="script.xsl"/>directive. If I use the absolute URL (http://localhost/mysite/script.xsl), it imports the style sheet fine; however, if I use the relative path (script.xsl) it reports "resource not found." I need to be able to port this amongst a set of machines, so I need to be able to use the relative URI. Any suggestions?

我正在尝试使用 VBScript 对 XML 对象进行 XSLT 转换。
我正在翻译的 XSL 文件包含<xsl:import href="script.xsl"/>指令。如果我使用绝对 URL ( http://localhost/mysite/script.xsl),它可以很好地导入样式表;但是,如果我使用相对路径 ( script.xsl),它会报告“找不到资源”。我需要能够在一组机器之间移植它,所以我需要能够使用相对 URI。有什么建议?

Notes:

笔记:

  • VBScript file is at http://localhost/myscript.asp
  • first XSL file is at http://localhost/mysite/styles.xsl
  • second XSL file is at http://localhost/mysite/script.xsl
  • using the relative path mysite/script.xslalso does not work
  • VBScript 文件位于 http://localhost/myscript.asp
  • 第一个 XSL 文件位于 http://localhost/mysite/styles.xsl
  • 第二个 XSL 文件位于 http://localhost/mysite/script.xsl
  • 使用相对路径mysite/script.xsl也不起作用

Addendum:

附录:

Thanks, everyone, for your answers. The more I dig into the code that is doing this, the stranger it is. myscript.aspis a rather unusual compilation of code. What happens is styles.xslis included in the HTML output of myscript.aspas an XML chunk (<xml src=...>) and then that chunk is loaded as a stylesheet, using VBScript, on the client side. This stylesheet is then used to transform an XML chunk that is retrieved via XMLHTTP. So the problem is the context of styles.xslis the HTML on the client side and has no relation to where script.xslis.

谢谢大家的回答。我越深入研究执行此操作的代码,它就越陌生。myscript.asp是一个相当不寻常的代码编译。发生的情况是作为 XML 块 ( )styles.xsl包含在 HTML 输出中,然后该块作为样式表加载,使用 VBScript,在客户端。然后使用此样式表转换通过 XMLHTTP 检索的 XML 块。所以问题是客户端的 HTML的上下文与 where 无关。myscript.asp<xml src=...>styles.xslscript.xsl

采纳答案by alumb

First Attempt:

第一次尝试:

I tried including script.xsl as another xml chunk and changing the import statement in every way I could imagine but without success.

我尝试将 script.xsl 作为另一个 xml 块并以我能想象的各种方式更改 import 语句,但没有成功。

Final solution:

最终解决方案:

Since the absolute url for includeing script.xsl worked from the beginning, my final solution was to convert style.xsl to style.asp with the correct doctype. In this file I was then able to retrieve the server name, protocol and path and echo them into the right place in the import statement using asp. Then, when this file got included in mysscript.asp, it had the correct absolute url for the server. This is a bit of a hack but the only way I found to solve this rather convoluted situation.

由于包含 script.xsl 的绝对 url 从一开始就起作用,我的最终解决方案是使用正确的文档类型将 style.xsl 转换为 style.asp。在这个文件中,我能够检索服务器名称、协议和路径,并使用 asp 将它们回显到导入语句中的正确位置。然后,当此文件包含在 mysscript.asp 中时,它具有服务器的正确绝对 url。这有点黑客,但我发现解决这种相当复杂的情况的唯一方法。

回答by Dominic Cronin

I would tackle this by running Sysinternals Process Monitor. With this tool running, you can actually see what files your script tries to open, even if they don't exist.

我会通过运行Sysinternals Process Monitor来解决这个问题。运行此工具后,您实际上可以看到脚本尝试打开哪些文件,即使它们不存在。

回答by Robert Rossney

The current directory for xsl:import, xsl:include, and the document() function is the directory containing the transform that uses them. So the xsl:import directive that you've said you're using ought to be working.

xsl:import、xsl:include 和 document() 函数的当前目录是包含使用它们的转换的目录。因此,您所说的 xsl:import 指令应该可以正常工作。

The only thing I can think of that might affect this: if you use a relative path, the file's being read directly from the file system, while if you use an absolute URI, it's being retrieved from the web server. Is it possible that there's some security setting that's preventing scripts from reading files in this directory?

我能想到的唯一一件事可能会影响这一点:如果您使用相对路径,则会直接从文件系统读取文件,而如果您使用绝对 URI,则会从 Web 服务器检索文件。是否有一些安全设置阻止脚本读取此目录中的文件?

回答by dacracot

@Jon I think you are very close... but shouldn't it be...

@Jon 我认为你非常接近......但不应该是......

<xsl:import href="/mysite/script.xsl"/>

...with a leading slash?

...带前导斜线?

回答by dacracot

I often run into this problem because there is a custom URI resolver being used by a library I can't see (or don't know about because I didn't read pertinent documentation.) I can't remember if this is spec or not but in the Saxon/java world, the custom URI resolver gets first crack at trying to resolve URI's for include/import statements as well as the document() function. If it can't resolve the URI, a default URI resolver gives it a try, which usually never misses when then URI is absolute.

我经常遇到这个问题,因为我看不到的库使用了一个自定义 URI 解析器(或者不知道,因为我没有阅读相关文档。)我不记得这是规范还是但在 Saxon/java 世界中,自定义 URI 解析器首先尝试解析包含/导入语句以及 document() 函数的 URI。如果它不能解析 URI,默认的 URI 解析器会给它一个尝试,当 URI 是绝对的时,它通常不会错过。

So, it's probably something in the ASP engine that is using a context driven URI resolver based on the app context.

因此,可能是 ASP 引擎中使用基于应用程序上下文的上下文驱动的 URI 解析器的某些东西。

回答by Jon Schneider

Is it possible that the "current directory" for purposes of the relative path might be the location of your ASP page, not your XSL file? In other words, if you haven't already, you might try:

用于相对路径的“当前目录”是否可能是您的 ASP 页面的位置,而不是您的 XSL 文件的位置?换句话说,如果你还没有,你可以尝试:

<xsl:import href="mysite/script.xsl"/>

回答by scunliffe

You need a variable that defines the approot, or webroot when loading JS, Image or CSS files.

在加载 JS、Image 或 CSS 文件时,您需要一个定义 approot 或 webroot 的变量。

 <xsl:import href="{$approot}/somedir/script.xsl"/>

or if you have the value in the XML,

或者如果您在 XML 中有值,

 <xsl:import href="{/root/@approot}/somedir/script.xsl"/>