Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Aug 13, 2010 @ 17:31
    Dan
    0

    Alternative XSLT template

    Hi,

    Just struggling with a multi-template XSLT script.  I have an XSLT script which loops and creates a table of data, but I want each item at level 3 to render in a different table template to items at level 4.

    I've got the first template working (so at the moment, the same table structure is just nested) but need a kick-start to know how to get it to render a different template when at level 4:

    <xsl:param name="currentPage"/>
    <xsl:template match="/">
      <xsl:call-template name="drawNodes">  
        <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=2]"/>  
      </xsl:call-template>
    </xsl:template>

    <xsl:template name="drawNodes">
      <xsl:param name="parent"/>
      <table class="template1">
        <tbody>
          <xsl:for-each select="$parent/* [@isDoc and string(umbracoNaviHide) != '1']">
            <tr>
              <td>
                <xsl:value-of select="@level"/>
                <xsl:value-of select="@nodeName"/>
                <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">   
                  <xsl:call-template name="drawNodes">    
                    <xsl:with-param name="parent" select="."/>    
                  </xsl:call-template>  
                </xsl:if>
              </td>
            </tr>
          </xsl:for-each>
        </tbody>
      </table>
    </xsl:template>

    Can anyone show basically how to do this, as I can't get my brain around it?

    Thanks!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 13, 2010 @ 17:51
    Lee Kelleher
    0

    Hi Dan,

    Have you got an example of the HTML you want to produce?  (I started writing an answer, but then realised that I might be going off on a tangent!)

    Cheers, Lee.

  • Dan 1288 posts 3921 karma points c-trib
    Aug 13, 2010 @ 18:00
    Dan
    0

    What I'm actually trying to produce is pretty convoluted, but something which would set me going would be like this:

    <table class="level3">
    <tr>
    <td>Level 3 category</td>
    <td></td>
    </tr>
    <tr>
    <td colspan="2">
    <table class="level4">
    <tr>
    <td>Sub-category</td>
    <td>Sub-category</td>
    <td>Sub-category</td>
    <td>Sub-category</td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td>Level 3 category</td>
    <td></td>
    </tr>
    <tr>
    <td colspan="2">
    <table class="level4">
    <tr>
    <td>Sub-category</td>
    <td>Sub-category</td>
    <td>Sub-category</td>
    <td>Sub-category</td>
    </tr>
    </table>
    </td>
    </tr>
    </table>

    So it's basically putting the stuff at level 3 in one kind of HTML table, then nesting the level 4 content in a completely different configuration of HTML table.

    Make sense?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 13, 2010 @ 18:25
    Lee Kelleher
    3

    Hi Dan,

    OK... get ready for this... it's going to blow your mind! haha

    <xsl:param name="currentPage" />
    
    <xsl:template match="/">
        <xsl:apply-templates select="$currentPage/ancestor-or-self::*[@level = 2]/*[@isDoc]" mode="table" />
    </xsl:template>
    
    <xsl:template match="*[umbracoNaviHide = 1]" />
    
    <xsl:template name="*[@isDoc]" mode="table">
        <table class="level{@level}">
            <xsl:apply-templates select="." mode="row" />
        </table>
    </xsl:template>
    
    <xsl:template name="*[@isDoc]" mode="row">
        <tr>
            <xsl:apply-templates select="." mode="cell" />
        </tr>
        <xsl:apply-templates select="*[@isDoc]" mode="row_colspan" />
    </xsl:template>
    
    <xsl:template name="*[@isDoc]" mode="row_colspan">
        <tr>
            <td colspan="2">
                <xsl:apply-templates select="." mode="table" />
            </td>
        </tr>
    </xsl:template>
    
    <xsl:template name="*[@level = 3]" mode="cell">
        <td>
            <xsl:value-of select="@level" />
            <xsl:value-of select="@nodeName" />
        </td>
        <td><!-- empty cell --></td>
    </xsl:template>
    
    <xsl:template name="*[@level = 4]" mode="cell">
        <td>
            <xsl:value-of select="@level" />
            <xsl:value-of select="@nodeName" />
        </td>
    </xsl:template>

    OK, there are a lot of templates here - following the logic it should like this...

    The first template selects @level=2, then apply-templates to one level below that (@level=3).

    Next @level3 is rendered as a <table>, then <tr>, then the <td> (with empty <td>, watch out for XML rendering i.e. <td/>).

    The "row" template then apply-templates for any child nodes (@level=4) with the "row_colspan" template - which then renders the "cell" templates.

     

    I haven't tested this, but worked through the logic and it "should" work.  Have I blown your mind? (I've gone cross-eyed!)

    Cheers, Lee.

  • Dan 1288 posts 3921 karma points c-trib
    Aug 13, 2010 @ 18:38
    Dan
    0

    Nice one, thanks Lee.  Yep, that's done it though.  My head is now well and truly frazzled!  Even more well and truly frazzled you might say :)

    Before I even try to get into this, it's giving me a pretty high level error:

    An 'xsl:template' element without a 'match' attribute cannot have a 'mode' attribute.

    Any ideas?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 13, 2010 @ 18:43
    Lee Kelleher
    3

    Opps! My bad - I went a bit copy-n-paste crazy! ... change the "name" attribute to "match" on the <xsl:templates>

    Cheers, Lee.

  • Dan 1288 posts 3921 karma points c-trib
    Aug 13, 2010 @ 18:48
    Dan
    0

    Now that last part actually does make sense, thanks.

    Lee you're an absolute XSLT legend.  Works a treat.  Thanks so much, and have a top weekend.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 13, 2010 @ 18:51
    Lee Kelleher
    0

    Awesome! Glad that it works.  Enjoy the weekend too... hopefully it wont rain too much! (in Bristol) :-$

Please Sign in or register to post replies

Write your reply to:

Draft