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?