我有一个HTML,我想用XSLT1.0解析这个文件,它是<file name="/var/application-data/..../application/controllers/cgu.php" />格式的
我希望它是<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>发布于 2017-05-05 10:55:46
一种方法是将第一个调用的结果(获取文件名)存储在变量中,然后第二次调用模板,但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>https://stackoverflow.com/questions/43801052
复制相似问题