使用 ../../的替代方法 [英] Alternative for using ../../

查看:52
本文介绍了使用 ../../的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的配置中,我有一个包含语句:

In my current configuration, I have an include statement as such:

<xsl:include href="../../../specialdata/anotherfolder/template.xslt"/>

我如何在这里表达:向上遍历路径,直到到达文件夹 'specialdata' 所在的级别"?

How can I make an expression here that says:"traverse up the path until you're at the level where also the folder 'specialdata' is"?

推荐答案

我不(完全)同意前面的答案,有一些方法可以实现这一点,标准(使用 XSLT 3.0)和处理器相关(使用旧处理器的 UriResolver).

I don't (quite) agree with the previous answers, there are ways to achieve this, both standard (with XSLT 3.0) and processor-dependent (using a UriResolver) for older processors.

通常,XSLT 处理器允许您从任何位置返回某些内容基于 href 属性,使用 UriResolver.如何创建、编译和配置此类组件取决于您使用的处理器.通常这意味着使用本机语言(Java、C++、C#)来实现接口并配置转换以使用它作为默认 UriResolver 的替代.

Typically, XSLT processors allow you to return something from any location based on the href attribute by using a UriResolver. It depends on the processor you use on how to create, compile and configure such a component. Usually it means using the native language (Java, C++, C#) to implement an interface and to configure the transformation to use this as an alternative to the default UriResolver.

在您为 UriResolver 编写的代码中,您可以做任何您想做的事情,包括遍历不同的父目录以检查部分 URI 是否匹配.

Inside the code you write for the UriResolver, you can do whatever you want, including traversing different parent directories for checking whether a partial URI matches.

请记住,XSLT 在这里不采用 URL,而是采用 URI.它的位置不是由 URI 预定义的,实际上,据说 URI 具有到物理位置的映射,可以是文件、内存中的 XML 树、数据库字段等.

Remember that XSLT does not take a URL here, it takes a URI. It's location is not predefined by the URI, in fact, the URI is said to have a mapping to a physical location, which can be a file, an in-memory XML tree, a database field etc.

或者,在 XSLT 3.0 中有一个具有阴影属性的跨平台解决方案(以下划线开头的属性可以采用在静态评估阶段处理的属性值模板).

Alternatively, in XSLT 3.0 there is a cross-platform solution with shadow attributes (attributes starting with an underscore that can take an attribute value template that is processed during the static evaluation phase).

shadow 属性的一个限制是它不能调用样式表函数(你用 xsl:function 声明的那些),但可以调用任何 XPath 函数,并且每个合法的 XPath 表达式也是合法的静态表达.

One limitation of shadow attributes is that it cannot call stylesheet functions (the ones you declare with xsl:function), but any XPath function can be called and every legal XPath expression is also a legal static expression.

以下适用于 XSLT 3.0,前提是静态可用文档"(可能受您的处理器限制)包括本地路径上的文档:

The following works in XSLT 3.0, provided that the "statically available documents" (which is possible restricted by your processor) include the documents on the local path:

<xsl:variable name="include" 
    static="yes" 
    select=" 'specialdata/anotherfolder/template.xslt' " />

<!-- note the underscore -->
<xsl:include _href="{
    ('../', '../../', '../../../') 
    [document-available(. || $include)][1]
    || $include }" />

上面的代码片段如下:

  • 创建一个静态变量,必须在使用之前声明,其中包含路径的已知部分
  • 创建一系列相对父路径
  • 当路径包含template.xslt
  • 时让谓词返回true
  • 返回第一个找到的
  • 连接(使用 XSLT 3.0 || 运算符)$include 变量的虚线路径
  • result 是一个包含包含文件的现有路径(或空序列,否则会产生错误)的字符串
  • shadow 属性的结果成为真实属性href的值.
  • create a static variable, which must be declared prior to its use, containing the known part of the path
  • create a sequence of relative parent paths
  • let the predicate return true when the path contains the template.xslt
  • return the first found
  • concatenate (using the XSLT 3.0 || operator) the dotted path to the $include variable
  • result is a string containing an existing path (or empty sequence otherwise, which will yield an error) to the include-file
  • the result of the shadow attribute becomes the value of the real attribute href.

请注意,如果没有扩展函数,则无法检查目录是否存在,但在您的情况下,这无关紧要,因为如果目录存在,则调用仅在文件也存在的情况下才有意义,因此我们可以以及检查整个路径.

Note, without extension functions it is not possible to check for a directory to exist, but in your case, it matters little, because if the directory exists, the call only makes sense if also the file exists, so we can just as well check the whole path.

支持影子属性的已知 XSLT 3.0 处理器有 Exselt (.NET) 和 Saxon(主要是 Java).其他 XSLT 3.0 处理器,如 XMLPrimeAltova Raptor(都非常部分地支持 XSLT 3.0)不支持 shadow 属性(目前).

Known XSLT 3.0 processors that support shadow attributes are Exselt (.NET) and Saxon (primarily Java). Other XSLT 3.0 processors like XMLPrime and Altova Raptor (both very partial XSLT 3.0 support) do not support shadow attributes (yet).

这篇关于使用 ../../的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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