Copied to clipboard

Flag this post as spam?

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


  • Sam 184 posts 209 karma points
    Feb 19, 2011 @ 12:07
    Sam
    0

    Merging two xslt files

    Hi everyone,

    I have the following two xslt files which clearly could be merged into one because the only difference is one displays 3 recent items, the other, all items.

    XSLT 1

    <xsl:template match="/">
                    <ul id="portfolio-list">
                      <xsl:apply-templates select="$currentPage/descendant-or-self::*[@isDoc]/PortfolioItem">
                      </xsl:apply-templates>
                    </ul>
    </xsl:template>

        <xsl:template match="PortfolioItem">
        <xsl:if test="position() &lt;= 3">
                                    <li>   
                                      <xsl:if test="position() mod 3 = 0">
    <xsl:attribute name="class">last</xsl:attribute>
    </xsl:if>

                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:apply-templates select="WebsitePreviewImage1" />
                    </a>                
    <div class="portfolio-box-shadow-sml"><xsl:comment>empty div holds shadow bg image</xsl:comment></div>
                                    </li>
        </xsl:if>

     </xsl:template>

    <!-- Template for the WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1">
            <xsl:variable name="media" select="umbraco.library:GetMedia(., false())" />
            <img alt="{$media/@nodeName}" src="{$media/umbracoFile}" width="240" />
    </xsl:template>

    <!-- Template for an empty WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1[not(normalize-space())]">
            <img src="/images/rollback.png" alt="No preview available" width="240" />
    </xsl:template>

    XSLT 2

    <xsl:template match="/">
                    <ul id="portfolio-list">
                      <xsl:apply-templates select="$currentPage/descendant-or-self::*[@isDoc]/PortfolioItem">
                        <xsl:sort select="./@createDate" order="descending" />
                      </xsl:apply-templates>
                    </ul>
    </xsl:template>

    <xsl:template match="PortfolioItem">
                                    <li>   
                                      <xsl:if test="position() mod 3 = 0">
    <xsl:attribute name="class">last</xsl:attribute>
    </xsl:if>

                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:apply-templates select="WebsitePreviewImage1" />
                    </a>                
            </li>

     </xsl:template>

    <!-- Template for the WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1">
            <xsl:variable name="media" select="umbraco.library:GetMedia(., false())" />
            <img alt="{$media/@nodeName}" src="{$media/umbracoFile}" width="240" />
    </xsl:template>

    <!-- Template for an empty WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1[not(normalize-space())]">
            <img src="/images/rollback.png" alt="No preview available" width="240" />
    </xsl:template>

    Can anyone suggest how to merge the two. So far I have this, which doesn't, shows nothing at all on the hompage. Still trying to get my head round templates:

    <xsl:template match="/">
                    <ul id="portfolio-list">
                      <xsl:choose>
                      <xsl:when test="$currentPage/@level=1">
                      <xsl:apply-templates select="$currentPage/descendant-or-self::*[@isDoc]/Homepage">
                        <xsl:sort select="./@createDate" order="descending" />
                      </xsl:apply-templates>
                      </xsl:when>
                        
                        <xsl:otherwise>                  
                      <xsl:apply-templates select="$currentPage/descendant-or-self::*[@isDoc]/PortfolioItem">
                        <xsl:sort select="./@createDate" order="descending" />
                      </xsl:apply-templates>
                        </xsl:otherwise>
                        
                      </xsl:choose>
                    </ul>
    </xsl:template>

    <xsl:template match="Homepage">
          <xsl:if test="position() &lt;= 3">
                                    <li>   
                                      <xsl:if test="position() mod 3 = 0">
    <xsl:attribute name="class">last</xsl:attribute>
    </xsl:if>

                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:apply-templates select="WebsitePreviewImage1" />
                    </a>                
            </li>
        </xsl:if>
     </xsl:template>
        
        <xsl:template match="PortfolioItem">
                                    <li>   
                                      <xsl:if test="position() mod 3 = 0">
    <xsl:attribute name="class">last</xsl:attribute>
    </xsl:if>

                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:apply-templates select="WebsitePreviewImage1" />
                    </a>                
            </li>

     </xsl:template>

    <!-- Template for the WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1">
            <xsl:variable name="media" select="umbraco.library:GetMedia(., false())" />
            <img alt="{$media/@nodeName}" src="{$media/umbracoFile}" width="240" />
    </xsl:template>

    <!-- Template for an empty WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1[not(normalize-space())]">
            <img src="/images/rollback.png" alt="No preview available" width="240" />
    </xsl:template>

    Thanks if anyone can shed some light :)

    Sam.

     

     

     

     

  • sun 403 posts 395 karma points
    Feb 19, 2011 @ 13:06
    sun
    0

    give a flag value to decide how many items you want to display.

    I used to do it by giving a number parameter.

    number=0 //show all

        <xsl:if test="$number=0 or position() &lt;= $number">

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Feb 19, 2011 @ 13:30
    Chriztian Steinmeier
    1

    Hi Sam,

    That's a good use case for the mode attribute on templates - try this (important stuff highlighted):

    <xsl:template match="/">
        <ul id="portfolio-list">
            <!-- Note that these are mutually exclusive with mode (scroll right!) -->
            <xsl:apply-templates select="$currentPage[@level = 1]/descendant-or-self::*[@isDoc]/PortfolioItem" mode="first3">
                <xsl:sort select="@createDate" order="descending"/>
            </xsl:apply-templates>
            <xsl:apply-templates select="$currentPage[not(@level = 1)]/descendant-or-self::*[@isDoc]/PortfolioItem">
                <xsl:sort select="@createDate" order="descending"/>
            </xsl:apply-templates>
        </ul>
    </xsl:template>
    
    <!-- Moded template for PortfolioItem elements -->
    <xsl:template match="PortfolioItem" mode="first3">
        <xsl:if test="position() &lt;= 3">
            <li>
                <xsl:if test="position() mod 3 = 0">
                    <xsl:attribute name="class">last</xsl:attribute>
                </xsl:if>
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:apply-templates select="WebsitePreviewImage1"/>
                </a>
            </li>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="PortfolioItem">
        <li>
            <xsl:if test="position() mod 3 = 0">
                <xsl:attribute name="class">last</xsl:attribute>
            </xsl:if>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:apply-templates select="WebsitePreviewImage1"/>
            </a>
        </li>
    </xsl:template>
    
    <!-- Template for the WebsitePreviewImage1 property -->
    <xsl:template match="WebsitePreviewImage1">
        <xsl:variable name="media" select="umbraco.library:GetMedia(., false())"/>
        <img alt="{$media/@nodeName}" src="{$media/umbracoFile}" width="240"/>
    </xsl:template>
    <!-- Template for an empty WebsitePreviewImage1 property -->
    
    <xsl:template match="WebsitePreviewImage1[not(normalize-space())]">
        <img src="/images/rollback.png" alt="No preview available" width="240"/>
    </xsl:template>
    

    /Chriztian

  • Sam 184 posts 209 karma points
    Feb 19, 2011 @ 14:18
    Sam
    0

    Hi Chriztian,

    I've seen you use the mode attribute before but never realised how or why to use it. Thanks a lot! Works perfectly :)

    Sam.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies