Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Jan 14, 2011 @ 10:09
    Anthony Candaele
    0

    applying template to list of documents

    Hi,

    I am working on a page that lists the documents from a folder in the media library.

    I was able to implement this page, but now I want to spice things up a little. I would like to have an extension-icon (pdf, .docx, .xslsx, etc ...) next to every document link. Therefore I wrap my <a href="..."> in a <span class="extension" like this:

    <span class="pdf"><a href="/media/585/presenatie-startdag.pdf">presentatie-startdag</a></span>

    To wrap my <a href="..."> in the proper <span class="[extension]"> tag I apply an xslt stylesheet template.

    This is my code:

    <xsl:param name="currentPage"/>
    <xsl:variable name="documents" select="umbraco.library:GetMedia(1088, 1)"/>
        
        <xsl:template match="/">
            <xsl:if test="count($documents) > 0">
              <table>
                <tr>
                  <th>Titel</th>
                  <th>Beschrijving</th>
                  <th>Aangepast op</th>
                  <th>Type</th>
                </tr>
                <xsl:for-each select="$documents/*">
                  <xsl:if test="./umbracoFile != ''">
                  <tr>
                    <td><xsl:apply-templates select="./umbracoExtension" /></td>
                    <td><xsl:value-of select="./description" /></td>
                    <td><xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'dd/MM/yyyy')" /></td>
                    <td><xsl:value-of select="./umbracoExtension" /></td>
                    </tr>
                    </xsl:if>
                  </xsl:for-each>
              </table>
            </xsl:if>
        </xsl:template>
        <xsl:template match="node[@umbracoExtension = 'pdf']">
          <span class="pdf"><a href="{@umbracoFile}" target="_blank"><xsl:value-of select="@nodeName" /></a></span>
        </xsl:template>

    But this doesn't work rendered output is this:


    As you see, under 'titel' does not appear the link to the document with an extension icon, but just the extension string

    I guess I'm doing something wrong, only I don't know what :) . Can anybody help me?

    Thanks, Anthony Candaele

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 14, 2011 @ 10:19
    Jan Skovgaard
    0

    Hi Anthony

    To make a link to the file you need to use the umbraco.library:NiceUrl(), which you're probably already familiar with from making navigation etc. and to get the name of the document I guess you should be using @nodeName instead of umbracoExtension? :-) Like this...

    <td><a href="umbraco.library:NiceUrl(./@id)"><xsl:value-of select="./@nodeName" /></a></td> - would this not do the trick? Or do I miss something?

    /Jan

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 14, 2011 @ 10:29
    Anthony Candaele
    0

    Hi Jan,

    Thanks for your help. I think the problem is related to the line where I try to implement my template:

    <xsl:apply-templates select="./umbracoExtension" />

    Because the processor doesn't find the right template:

    <xsl:template match="node[@umbracoExtension = 'pdf']">
          <span class="pdf"><xsl:value-of select="./@nodeName" />span>
        xsl:template>

    It just applies the default template which is the string value of the umbracoExtension element

    So to fix my problem I should find a way to apply the template

    <xsl:template match="node[@umbracoExtension = 'pdf']">
          <span class="pdf"><xsl:value-of select="./@nodeName" />span>
        xsl:template>

    Thanks for your help,

    Anthony Candaele

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 14, 2011 @ 11:07
    Anthony Candaele
    0

    Ok, now I found out how I can apply my template:

    <xsl:param name="currentPage"/>
    <xsl:variable name="documents" select="umbraco.library:GetMedia(1088, 1)"/>
            
        <xsl:template match="/">
            <xsl:if test="count($documents) > 0">
              <table>
                <tr>
                  <th>Titel</th>
                  <th>Beschrijving</th>
                  <th>Aangepast op</th>
                  <th>Type</th>
                </tr>
                <xsl:for-each select="$documents/*">
                  <xsl:if test="./umbracoFile != ''">
                  <tr>
                    <td><span class="<xsl:apply-templates select='umbracoExtension' />"><a href="./umbracoFile"><xsl:value-of select="./nodeName" /></a></span></td>
                    <td><xsl:value-of select="./description" /></td>
                    <td><xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'dd/MM/yyyy')" /></td>
                    <td><xsl:value-of select="./umbracoExtension" /></td>
                    </tr>
                    </xsl:if>
                  </xsl:for-each>
              </table>
            </xsl:if>
        </xsl:template>
        <xsl:template match="umbracoExtension ['pdf']">
          pdf
        </xsl:template>

    But the xslt-processor is complaining about this line:

    <td><span class="<xsl:apply-templates select='umbracoExtension' />"><a href="./umbracoFile"><xsl:value-of select="./nodeName" /></a></span></td>

    the error message is:

    System.Xml.XmlException: '<', hexadecimal value 0x3C, is an invalid attribute character. Line 28, position 34.

    Anyone has an idea how I can fix this error?

    Thanks for your help,

    Anthony Candaele


  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 14, 2011 @ 15:15
    Jan Skovgaard
    1

    Hi Anthony

    That's because you cannot use xslt code inside an attribute...

    Instead do this

    <td>
    <
    span>
    <xsl:attribute name="class">
    <xsl:apply-templates select='umbracoExtension' /></xsl:attribute>
    <
    a href="./umbracoFile"><xsl:value-of select="./nodeName" /></a></span>
    </
    td>

    This should do the trick.

    /Jan

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 14, 2011 @ 16:00
    Anthony Candaele
    0

    Oh I see,

    I had already tried something else, using a  <xsl:choose> statement:

    <xsl:param name="currentPage"/>
    <xsl:variable name="documents" select="umbraco.library:GetMedia(1088, 1)"/>
             
        <xsl:template match="/">
            <xsl:if test="count($documents) > 0">
              <table>
                <tr>
                  <th>Titelth>
                  <th>Beschrijvingth>
                  <th>Aangepast opth>
                  <th>Typeth>
                tr>
                <xsl:for-each select="$documents/*">
                  <xsl:if test="./umbracoFile != ''">
                  <tr>
                    <td>
                      <xsl:choose>
                        <xsl:when test="./umbracoExtension = 'pdf'">                    
                          <span class="pdf"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        <xsl:when test="./umbracoExtension = 'doc'">
                          <span class="doc"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        <xsl:when test="./umbracoExtension = 'docx'">
                          <span class="doc"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        <xsl:when test="./umbracoExtension = 'xls'">
                          <span class="xsl"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        <xsl:when test="./umbracoExtension = 'xlsx'">
                          <span class="xls"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        <xsl:when test="./umbracoExtension = 'ppt'">
                          <span class="ppt"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        <xsl:when test="./umbracoExtension = 'pptx'">
                          <span class="ppt"><a href="{./umbracoFile}"><xsl:value-of select="./@nodeName" />a>span>
                        xsl:when>
                        xsl:choose>
                    td>
                    <td><xsl:value-of select="./description" />td>
                    <td><xsl:value-of select="umbraco.library:FormatDateTime(./@createDate, 'dd/MM/yyyy')" />td>
                    <td><xsl:value-of select="./umbracoExtension" />td>
                    tr>
                    xsl:if>
                  xsl:for-each>
              table>
            xsl:if>
        xsl:template>

    But I'll try your suggestion and let you know if it worked.

    This is the result of my documents page:


  • Anthony Candaele 1197 posts 2049 karma points
    Jan 14, 2011 @ 16:26
    Anthony Candaele
    0

    Hi Jan,

    I tried out your solution and it worked perfectly. It's more elegant and less verbose than using an <xsl:choose> statement.

    The only problem I had was when the document had and office 2007 extension (xlsx, docx, pptx). The stylesheet doesn't have style definitions for these extensions:

    span.pdf { background-image:url(/images/algemeen/pdf.gif); }
    span.external { background-image:url(/images/algemeen/externesite.gif); }
    span.doc { background-image:url(/images/algemeen/doc.gif); }
    span.ppt { background-image:url(/images/algemeen/ppt.gif); }
    span.xls { background-image:url(/images/algemeen/xls.gif); }
    span.audio { background-image:url(/images/algemeen/aud.gif); }
    span.video { background-image:url(/images/algemeen/vid.gif); }
    span.beveiligd { background-image:url(/images/algemeen/sleutel.gif); }

    So I tried to create some new style definitions by just copying the above lines:

    span.docx { background-image:url(/images/algemeen/doc.gif); }

    etc ...

    But for some reason (cache?) this didn't work, so I had to revert back to <xsl:choose> implementation.

    Thanks anyway for your excellent help!

    Anthony

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 14, 2011 @ 19:06
    Jan Skovgaard
    0

    Hi Anthony

    Happy to hear you found a solution.

    Unless there are strong requirements that the icons you're displaying *must* be present in older versions than IE8 you can actually use CSS selectors to do this.

    Using attributes selectors you can write the following in your CSS

    a[href=$='.pdf']{background:url(yourpath) no-repeat}
    a[href=$='.doc']{background:url(yourpath) no-repeat}

    etc.

    If you need to make sure that it's also shown in IE7 and 6 etc. you can use conditional comments to include a javascript file where you can set the icons using jquery. There should be a fair amount of examples on that on google.

    Are "external" and "beveiligd" some custom filetypes, or what is the idea with those? Maybe there is another CSS selector you can use to match these two exclusively as well - but since I'm not sure what they're for I can't advise you on that :-)

    You got a solution that is working but if time allows it and you're interested I really think you should give this a go. It makes your XSLT code cleaner and the renderen HTML output too.

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft