在 XSLT 中进行文件路径操作

发布于 2024-09-07 12:33:40 字数 998 浏览 0 评论 0原文

我希望生成的输出文件包含指向相对于样式表的路径的文件路径。样式表的位置可以更改,并且我不想使用样式表的参数。我的解决方案是获取完整的样式表 URI:

<xsl:variable name="stylesheetURI" select="document-uri(document(''))" />

现在我只需要从 $stylesheetURI 中截取文件名。这启发我编写 PHP 函数 basenamedirname

<xsl:function name="de:basename">
    <xsl:param name="file"></xsl:param>
    <xsl:sequence select="tokenize($file, '/')[last()]" />
</xsl:function>

<xsl:function name="de:dirname">
    <xsl:param name="file"></xsl:param>
    <xsl:sequence 
        select="string-join(tokenize($file, '/')[position() != last()], '/')" />
</xsl:function>

现在我可以在我的模板中执行类似的操作:

<img src="{concat(de:dirname($stylesheetURI),'/img/myimage,png')}" />

我的问题是:是否有更好/更快的方法来使用本机 XSLT 2.0 来完成此操作?

I'd like my generated output file to contain file paths that point to a path relative to the stylesheet. The location of the stylesheet can change and I don't want to use a parameter for the stylesheet. My solution for this is to get the full stylesheet URI:

<xsl:variable name="stylesheetURI" select="document-uri(document(''))" />

Now I only need to cut off the filename from $stylesheetURI. This has inspired me to write XSLT 2.0 clones of the PHP functions basename and dirname:

<xsl:function name="de:basename">
    <xsl:param name="file"></xsl:param>
    <xsl:sequence select="tokenize($file, '/')[last()]" />
</xsl:function>

<xsl:function name="de:dirname">
    <xsl:param name="file"></xsl:param>
    <xsl:sequence 
        select="string-join(tokenize($file, '/')[position() != last()], '/')" />
</xsl:function>

Now I can do something like this in my template:

<img src="{concat(de:dirname($stylesheetURI),'/img/myimage,png')}" />

My question is: Are there better/faster ways to accomplish this with native XSLT 2.0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2024-09-14 12:33:40

我测试了(不是太广泛)这些函数,它们执行速度似乎比提供的快了 25%。当然,结果取决于字符串长度和限定符的数量:

  <xsl:function name="de:basename" as="xs:string">
    <xsl:param name="pfile" as="xs:string"/>
    <xsl:sequence select=
     "de:reverseStr(substring-before(de:reverseStr($pfile), '/'))
     " />
  </xsl:function>

  <xsl:function name="de:dirname" as="xs:string">
    <xsl:param name="pfile" as="xs:string"/>
    <xsl:sequence select=
     "de:reverseStr(substring-after(de:reverseStr($pfile), '/'))
     " />
  </xsl:function>

  <xsl:function name="de:reverseStr" as="xs:string">
    <xsl:param name="pStr" as="xs:string"/>

    <xsl:sequence select=
    "codepoints-to-string(reverse(string-to-codepoints($pStr)))"/>
  </xsl:function>

I tested (not too extensively) these functions and they seem to perform 25% faster than the provided. Of course, the results depend on the string length and the number of qualifiers:

  <xsl:function name="de:basename" as="xs:string">
    <xsl:param name="pfile" as="xs:string"/>
    <xsl:sequence select=
     "de:reverseStr(substring-before(de:reverseStr($pfile), '/'))
     " />
  </xsl:function>

  <xsl:function name="de:dirname" as="xs:string">
    <xsl:param name="pfile" as="xs:string"/>
    <xsl:sequence select=
     "de:reverseStr(substring-after(de:reverseStr($pfile), '/'))
     " />
  </xsl:function>

  <xsl:function name="de:reverseStr" as="xs:string">
    <xsl:param name="pStr" as="xs:string"/>

    <xsl:sequence select=
    "codepoints-to-string(reverse(string-to-codepoints($pStr)))"/>
  </xsl:function>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文