在XSLT中执行文件路径操作 [英] Doing file path manipulations in XSLT

查看:113
本文介绍了在XSLT中执行文件路径操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(''))" />

现在我只需要从 $ stylesheetURI 。这激发了我编写PHP函数基本名 dirname

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')}" />

我的问题是:是否有更好/更快的方式来使用原生XSLT 2.0来完成这项工作?

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

推荐答案

我测试了这些功能 ,而且似乎以25% / strong>比提供的。当然,结果取决于字符串长度和限定符的数量:

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>

这篇关于在XSLT中执行文件路径操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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