2017-05-05 78 views
0

我有一个HTML文件,我想用XSLT 1.0 文件的格式为<file name="/var/application-data/..../application/controllers/cgu.php" />XSL解析文件名文件夹

解析,我想这是 <file filename="cgu.php" folderName="controller/"/>

我管理这个使用<xsl:value-of select="substring-before(substring(@name, 85), '/')"/>做 ,但我希望它适用于所有长度的路径。 是否可以计算“/”的出现次数来检索路径的最后部分?

我用递归模板只检索文件名,但我需要的文件夹名称也

<xsl:template name="substring-after-last"> 
     <xsl:param name="string"/> 
     <xsl:choose> 
      <xsl:when test="contains($string, '/')"> 
       <xsl:call-template name="substring-after-last"> 
        <xsl:with-param name="string" select="substring-after($string, '/')"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$string"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
+0

参见:http://stackoverflow.com/questions/38848374/how-to-find-最后的预览工作和存储到我的领域在xslt/38848925#38848925和http://stackoverflow.com/questions/41624974/xsl-display-attribute-after-a-certain-字符/ 41625340#41625340 –

回答

0

一种方式做到这一点,是存储第一次调用的结果(获取文件名)在一个变量中,并且第二次调用模板,但name属性被文件名的长度截断(所以文件夹名称现在是最后一项)。

<xsl:template match="file"> 
    <xsl:variable name="fileName"> 
     <xsl:call-template name="substring-after-last"> 
      <xsl:with-param name="string" select="@name" /> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:variable name="folderName"> 
     <xsl:call-template name="substring-after-last"> 
      <xsl:with-param name="string" select="substring(@name, 1, string-length(@name) - string-length($fileName) - 1)" /> 
     </xsl:call-template> 
    </xsl:variable> 
    <file filename="{$fileName}" folderName="{$folderName}/"/> 
</xsl:template> 

另外,如果你的程序支持的话,你可以到别的EXSLT的tokenize功能

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:str="http://exslt.org/strings" 
    extension-element-prefixes="str"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="file"> 
     <xsl:variable name="parts" select="str:tokenize(@name, '/')" /> 
     <file filename="{$parts[last()]}" folderName="{$parts[last() - 1]}/"/> 
    </xsl:template> 
</xsl:stylesheet>