Xslt 根据字符串的结尾显示不同的xsl:attribute
我在xsl文档中有以下xsl代码Xslt 根据字符串的结尾显示不同的xsl:attribute,xslt,xpath,Xslt,Xpath,我在xsl文档中有以下xsl代码 <A target="_blank" style="text-decoration=none"> <xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline</xsl:attribute>
<A target="_blank" style="text-decoration=none">
<xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline</xsl:attribute>
<xsl:attribute name="prefix"><xsl:value-of select="FileName"/>: </xsl:attribute>
<IMG src="images/word_small.gif" border="0"/>
</A>
现在,这对word文档很有效。但是,我需要一种在代码中检查文件扩展名的方法,并根据If语句显示正确的Image和xsl:属性
因此,If语句如下所示:-
If correctedPath.ToLower.Contains(".doc") Then
//display the word icon and attributes
Else
//display the excel icon and attributes
End If
你能给我一些建议和帮助我如何做到这一点吗
谢谢如果需要,这可以在XSLT文档中完成。为了显示图像,可以使用xsl:choose语句来测试URLFilePath元素
<xsl:choose>
<xsl:when test="contains(., '.doc')">
<IMG src="images/word_small.gif" border="0"/>
</xsl:when>
<xsl:when test="contains(., '.xls')">
<IMG src="images/excel_small.gif" border="0"/>
</xsl:when>
</xsl:choose>
然后,在xls中,您可以简单地使用该属性设置图像的源属性
<IMG border="0">
<xsl:attribute name="src"><xsl:value-of select='@image' /></xsl:attribute>
</IMG>
如果需要,这可以在XSLT文档中完成。为了显示图像,可以使用xsl:choose语句来测试URLFilePath元素
<xsl:choose>
<xsl:when test="contains(., '.doc')">
<IMG src="images/word_small.gif" border="0"/>
</xsl:when>
<xsl:when test="contains(., '.xls')">
<IMG src="images/excel_small.gif" border="0"/>
</xsl:when>
</xsl:choose>
然后,在xls中,您可以简单地使用该属性设置图像的源属性
<IMG border="0">
<xsl:attribute name="src"><xsl:value-of select='@image' /></xsl:attribute>
</IMG>
仅使用
contains()
通常会产生错误的结果(请参阅测试XML文档)
所需的是一个函数,该函数在XPath 2.0中是标准的,可以在XSLT 1.0中实现,如下所示:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="URLFilePath">
<xsl:variable name="visDoc">
<xsl:call-template name="ends-with">
<xsl:with-param name="pEnding" select="'.doc'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="visXls">
<xsl:call-template name="ends-with">
<xsl:with-param name="pEnding" select="'.xls'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$visDoc=1">word_small.gif</xsl:when>
<xsl:when test="$visXls=1">xls_small.gif</xsl:when>
<xsl:otherwise>unknown_small.gif</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ends-with">
<xsl:param name="pEnding"/>
<xsl:value-of select=
"number(substring(.,
string-length() -string-length($pEnding) +1
)
=
$pEnding
)
"/>
</xsl:template>
</xsl:stylesheet>
请注意仅使用
contains()
会产生不正确的结果。仅使用contains()
通常会产生错误的结果(请参阅测试XML文档)
所需的是一个函数,该函数在XPath 2.0中是标准的,可以在XSLT 1.0中实现,如下所示:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="URLFilePath">
<xsl:variable name="visDoc">
<xsl:call-template name="ends-with">
<xsl:with-param name="pEnding" select="'.doc'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="visXls">
<xsl:call-template name="ends-with">
<xsl:with-param name="pEnding" select="'.xls'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$visDoc=1">word_small.gif</xsl:when>
<xsl:when test="$visXls=1">xls_small.gif</xsl:when>
<xsl:otherwise>unknown_small.gif</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ends-with">
<xsl:param name="pEnding"/>
<xsl:value-of select=
"number(substring(.,
string-length() -string-length($pEnding) +1
)
=
$pEnding
)
"/>
</xsl:template>
</xsl:stylesheet>
请注意仅使用
contains()
会产生不正确的结果。我设法想出了一个解决方案!很抱歉回复晚了,但我还得做点别的
代码如下:-
<A target="_blank" style="text-decoration=none">
<xsl:choose>
<xsl:when test="contains(., '.doc')">
<xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline
</xsl:attribute>
<xsl:attribute name="prefix">
<xsl:value-of select="FileName"/>:
</xsl:attribute>
<IMG src="images/word_small.gif" border="0"/>
</xsl:when>
<xsl:when test="contains(., '.xls')">
<xsl:attribute name="href">viewxls.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline
</xsl:attribute>
<xsl:attribute name="prefix">
<xsl:value-of select="FileName"/>:
</xsl:attribute>
<IMG src="images/excel_small.gif" border="0"/>
</xsl:when>
</xsl:choose>
</A>
谢谢大家的帮助,真的非常感谢 我终于想出了一个解决办法!很抱歉回复晚了,但我还得做点别的 代码如下:-
<A target="_blank" style="text-decoration=none">
<xsl:choose>
<xsl:when test="contains(., '.doc')">
<xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline
</xsl:attribute>
<xsl:attribute name="prefix">
<xsl:value-of select="FileName"/>:
</xsl:attribute>
<IMG src="images/word_small.gif" border="0"/>
</xsl:when>
<xsl:when test="contains(., '.xls')">
<xsl:attribute name="href">viewxls.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline
</xsl:attribute>
<xsl:attribute name="prefix">
<xsl:value-of select="FileName"/>:
</xsl:attribute>
<IMG src="images/excel_small.gif" border="0"/>
</xsl:when>
</xsl:choose>
</A>
谢谢大家的帮助,真的非常感谢 我的回复很晚,但我找到了两个答案,这两个答案涉及到如何将字符串结尾与XSLT 1.0匹配,而且非常优雅:
去给他们+1一个迟来的回复,但我找到了两个答案,它们处理的是字符串末尾与XSLT 1.0的匹配,非常优雅:
去给他们+1嗨,蒂姆,非常感谢你的帮助。你也喜欢这项工作吗?嗨,约翰。是的,像这样的东西会有用的。您可以在xsl:when中添加任意数量的元素。但是,如果可以的话,您应该尽量避免重复代码,只对不同的位使用xsl:choose。
contains
仅检查子字符串是否出现在目标字符串中的任何位置,不知道它是否出现在目标字符串的末尾。嗨,蒂姆,非常感谢你的帮助。你也喜欢这个工作吗?嗨,约翰。是的,像这样的东西会有用的。您可以在xsl:when中添加任意数量的元素。但是,如果可以的话,您应该尽量避免重复代码。xsl:choose仅用于不同的位。contains
仅检查子字符串是否出现在目标字符串的任何位置,而不是是否出现在目标字符串的末尾。问得好(+1)。有关正确的解决方案,请参见我的答案。:)好问题(+1)。有关正确的解决方案,请参见我的答案。:)嗨,Dimitre,现在我有viewdoc.aspx?doc=&;mode=inline:这似乎是可行的。那么我该如何适应你的例子呢?@Johann:这不适用于我在回答中提供的XML文档:myFile.doc myFile.xls myFile.xls.doc myFile.doc.xls
Hi Dimitre此时我有viewdoc.aspx?doc=&;mode=inline:这似乎是可行的。那么我该如何适应你的例子呢?@Johann:这不适用于我在回答中提供的XML文档:myFile.doc myFile.xls myFile.xls.doc myFile.doc.xls