代码之家  ›  专栏  ›  技术社区  ›  JMon

显示不同的xsl:attribute depending 在字符串的结尾

  •  2
  • JMon  · 技术社区  · 16 年前

    我在xsl文档中有以下xsl代码

                    <A target="_blank" style="text-decoration=none">
                        <xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&amp;mode=inline</xsl:attribute>
                            <xsl:attribute name="prefix"><xsl:value-of select="FileName"/>: </xsl:attribute>
              <IMG src="images/word_small.gif" border="0"/>
                    </A>
    

    在我做这件事背后的代码里

                newItemNode = xmlDocument.CreateElement("URLFilePath")
                newItemNode.InnerText = correctedPath
                xmlItemNode.ParentNode.AppendChild(newItemNode)
    

    现在,这对word文档很有用。但是,我需要一种方法在代码中检查文件的扩展名,并显示正确的图像和xsl:attribute depending 在If语句中。

    所以If语句是这样的this:-

                If correctedPath.ToLower.Contains(".doc") Then
                     //display the word icon and attributes
                Else
                     //display the excel icon and attributes
                End If
    

    谢谢

    4 回复  |  直到 16 年前
        1
  •  2
  •   Dimitre Novatchev    16 年前

    只是使用 contains() 通常可能产生错误的结果 (请参阅测试XML文档)。

    必要的是 ends-with() 函数,该函数在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>
    

    :

    <files>
     <URLFilePath>myFile.doc</URLFilePath>
     <URLFilePath>myFile.xls</URLFilePath>
     <URLFilePath>myFile.xls.doc</URLFilePath>
     <URLFilePath>myFile.doc.xls</URLFilePath>
    </files>
    

    产生了正确的结果 :

     word_small.gif
     xls_small.gif
     word_small.gif
     xls_small.gif
    

    那只是利用 包含()

        2
  •  0
  •   JMon    16 年前

    我设法想出了一个解决办法!很抱歉回信迟了,但我得做点别的

    这是你的名字code:-

                    <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"/>&amp;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"/>&amp;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>
    

    谢谢大家的帮助,真的非常感谢!

        4
  •  -2
  •   Tim C    16 年前

    如果需要,这可以在XSLT文档中完成。为了显示图像,可以使用xsl:choose statement,测试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>
    

    imageAttr = xmlDocument.CreateAttr("image")     
    If correctedPath.ToLower.Contains(".doc") Then
        imageAttr.value = "images/word_small.gif"
    Else
        imageAttr.value = "images/excel_small.gif"
    End If
    newItemNode.AppendChild(imageAttr) 
    

    然后,在xls中,您可以简单地使用这个属性来设置图像的源属性

    <IMG border="0"> 
       <xsl:attribute name="src"><xsl:value-of select='@image' /></xsl:attribute>
    </IMG>