views:

26

answers:

1

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?

+3  A: 

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>
Dimitre Novatchev
@Dimitre: Wow! Could it be this because sequence implementation (with tokenize and string-join) have more porform costs than string manipulation? But, doesn't string-to-codeponits also return a sequence?
Alejandro
@Alejandro: tokenize() does a lot of work -- scanning char by char and placing every token in a separate item. Wherever substring-before() or substring-after() only scan up to the first occurence.
Dimitre Novatchev