我正在寻找将当前工作目录存储到变量中的 XSL 样式表的具体示例。
我需要这个,因为在我的情况下,我需要使用相对路径导入某些库样式表。知道我的处理器选择的路径作为当前目录就足够了。
编辑
请不要提供特定于供应商的信息。
最佳答案
在 XSLT 2.0 中,可以使用标准的 XPath 2.0 函数 resolve-uri() .
请注意,包含/导入的样式表模块的相对 URI 是相对于包含/导入样式表模块的基本 URI 的 - 而不是来自“工作目录”!
以下是 W3 F&O 规范中对此函数的部分描述:
8.1 fn:resolve-uri
fn:resolve-uri($relative as xs:string?) as xs:anyURI?
fn:resolve-uri($relative as xs:string?, $base as xs:string) as xs:anyURI?
Summary: The purpose of this function is to enable a relative URI to be resolved against an absolute URI.
The first form of this function resolves $relative against the value of the base-uri property from the static context. If the base-uri property is not initialized in the static context an error is raised [err:FONS0005].
这是一个非常简单的例子:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:sequence select=
"resolve-uri('resolve-uri-example2.xsl')"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于任何 xml 文档(未使用)时,结果是:
file:///c:/tests/resolve-uri-example2.xsl
这是正确的结果,因为我们的主要样式表模块保存为:
c:/tests/resolve-uri-example2.xsl
关于xml - XSL,获取当前工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910389/