Developer forum

Forum » Templates » Multiple templates in XSLT based on page level

Multiple templates in XSLT based on page level

Nikki Strømsnes
Reply

Long story short, I'm creating a three level navigation, which require different markup for each. "No sweat", said the optimistic developer, "I'll just hack it together with XSLT. I mean, how hard can it be?". This was my first mistake.

 

<xsl:template match="/NavigationTree">    
    <xsl:if test="count(//Page) > 0">
      <ul class="floatLeft">

        <xsl:apply-templates select="Page">
          <xsl:with-param name="depth" select="1"/>
        </xsl:apply-templates>

      </ul>
    </xsl:if>
  </xsl:template>


  <xsl:template match="//Page">
    <xsl:param name="depth"/>
    <li>

      <xsl:if test="count(Page)">
        <ul>
          <xsl:apply-templates select="Subpage">
            <xsl:with-param name="depth" select="$depth+1"/>
          </xsl:apply-templates>
        </ul>
      </xsl:if>

    </li>
  </xsl:template>


  <xsl:template match="//Subpage">
    <xsl:param name="depth"/>
    <li class="test">
    </li>
  </xsl:template>

I know this is not the way to do it, but my initial logic was this:
For the first level, use the "Page" template.
If the depth is more than 1, use the "Subpage" template.
(Later, I would also need a third template, but let's not worry about that right now)

As you have probably already gathered, I'm a complete rookie when it comes to XSLT, but I hope you can see what I'm trying to achieve. My question is, is it even possible, and how would I go about it?


Replies

 
Nikki Strømsnes
Reply
 
 
Vladimir
Reply
Hi Nikki,
you may use parameter depth or page's attribute @AbsoluteLevel  to achive this:

  <xsl:template match="/NavigationTree">

    <ul class="floatLeft">
      <xsl:apply-templates select="Page">
        <xsl:with-param name="depth" select="1"/>
      </xsl:apply-templates>
    </ul>

  </xsl:template>

  <xsl:template match="Page">
    <xsl:param name="depth"/>
     <li>
        <!-- or "$depth=1" -->
        <xsl:if test="@AbsoluteLevel=1">
          <xsl:value-of select="Level 1 markup" disable-output-escaping="yes"/>
          ...
        </xsl:if>
        <xsl:if test="@AbsoluteLevel=2">
          <xsl:value-of select="Level 2 markup" disable-output-escaping="yes"/>
          ...
        </xsl:if>

        <xsl:if test="count(Page) > 0">
          <ul>
          <xsl:apply-templates select="Page">
            <xsl:with-param name="depth" select="$depth+1"/>
          </xsl:apply-templates>
          </ul>
        </xsl:if>
     </li>
  </xsl:template>
Best regards,
Vladimir

 
Nikki Strømsnes
Reply
Thanks Vladimir. I found a different solution:

    <xsl:template match="/NavigationTree">

    <xsl:if test="count(//Page) > 0">
        <xsl:apply-templates select="Page">
          <xsl:with-param name="depth" select="1"/>
        </xsl:apply-templates>
    </xsl:if>

  </xsl:template>  

  <xsl:template match="//Page">
    <xsl:param name="depth"/>
      <a>
        <xsl:attribute name="href"><xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/></xsl:attribute>
          <xsl:value-of select="@MenuText" disable-output-escaping="yes"/>
      </a>


      <xsl:if test="count(./Page)">

        <xsl:for-each select="./Page">

          <xsl:value-of select="@MenuText" disable-output-escaping="yes"/>

            <xsl:for-each select="./Page">
                <a href="{@FriendlyHref}">
                  <xsl:value-of select="@MenuText" disable-output-escaping="yes"/>
                </a>
            </xsl:for-each>

         </xsl:for-each>

        </xsl:if>

  </xsl:template>
 If for nothing else, I did learn a bit about XSLT, but it's safe to say it's not one of my strong points. :)

 

You must be logged in to post in the forum