在 XSLT 中进行文件路径操作
我希望生成的输出文件包含指向相对于样式表的路径的文件路径。样式表的位置可以更改,并且我不想使用样式表的参数。我的解决方案是获取完整的样式表 URI:
<xsl:variable name="stylesheetURI" select="document-uri(document(''))" />
现在我只需要从 $stylesheetURI
中截取文件名。这启发我编写 PHP 函数 basename 和 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>
现在我可以在我的模板中执行类似的操作:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我测试了(不是太广泛)这些函数,它们执行速度似乎比提供的快了 25%。当然,结果取决于字符串长度和限定符的数量:
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: