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
. 这启发我编写了XSLT2.0版本的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')}" />
我的问题是:有没有更好/更快的方法可以通过原生的XSLT2.0来实现这一点?