tags:

views:

228

answers:

3

Dear All,

I have the following xsl code in an xsl document

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

and in the code-behind I am doing this

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

Now that works fine for word documents. However I need a way in code to check the extension of the file, and display the correct Image and xsl:attribute depending on the If statement.

So the If statement will be like this:-

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

Can you please give me some tips and help on how I can achieve this?

Thanks

A: 

This can be done purely in your XSLT document if you require. For displaying the image, you could use a xsl:choose statement, that tests the URLFilePath element

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

If you wanted to do this check in the code behind, you could always add extra attributes to your URLFilePath element.

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) 

And then, in your xls, you can simply use this attribute to set the source attribute of the image

<IMG border="0"> 
   <xsl:attribute name="src"><xsl:value-of select='@image' /></xsl:attribute>
</IMG>
Tim C
Hi TimThanks a lot for your helpWould something like this work also?
Johann
Johann
Johann
Hi Johann. Yep, something like that would work. You can next any amount of elements within the xsl:when. You should try and avoid repeating code though, are use the xsl:choose for only the bits that differ, if you can.
Tim C
`contains` only checks whether the substring occurs anywhere in the target string, not whether it appears at the *end* of the target string.
markusk
+2  A: 

Just using contains() may generally produce the wrong results (see the test XML document).

What is necessary is a ends-with() function, which is standard in XPath 2.0 and can be implemented in XSLT 1.0 as in the following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <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>

When this transformation is applied on the following test XML document:

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

the correct result is produced:

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

Do note that just using contains() produces incorrect results.

Dimitre Novatchev
Johann
<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> </i18n:popup> </A>and this seems to be working. So how can I adapt to your example?
Johann
@Johann: This is *not* working with the XML document that I provided in my answer: `<files> <URLFilePath>myFile.doc</URLFilePath> <URLFilePath>myFile.xls</URLFilePath> <URLFilePath>myFile.xls.doc</URLFilePath> <URLFilePath>myFile.doc.xls</URLFilePath> </files>`
Dimitre Novatchev
A: 

Hi all

I managed to come up with a solution! Sorry for the late reply but had to work on something else

Here is the 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>

Thanks for all your help guys, really very much appreciated!

Johann