代码之家  ›  专栏  ›  技术社区  ›  chiborg Alessandro Minoccheri

在XSLT中执行文件路径操作

  •  6
  • chiborg Alessandro Minoccheri  · 技术社区  · 14 年前

    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来实现这一点?

    1 回复  |  直到 14 年前
        1
  •  7
  •   Dimitre Novatchev    14 年前

    我测试过(不太广泛) 这些功能 而且他们 表现似乎快了25% 比提供的要多。当然,结果取决于字符串长度和限定符的数量:

      <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>