Nix
Nix

Reputation: 5998

Multiple templates in XSLT based on page level

I am working with a CMS, which allows the use of XSLT for the navigation. The design dictates a navigation with three different levels, and with somewhat different markup for each one. "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.

Let's cut to the chase:

XSLT

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

My idea is, that the first level should use the "Page" template, and all it's subpages should use the "Subpage" template. Instead, all the items after the second in the first level, use the "Subpage" template. If I remove that template, it correctly displays the whole first level — but no sublevels. My uneducated guess is that something is wrong in the if statement inside the Page template.

Does that make sense?

I know HTML better than my native spoken language, but I'm a rookie when it comes to XSLT. I couldn't even figure what to search for, so if this problem has been covered somewhere else, guide me O' mighty wise men.

Upvotes: 1

Views: 877

Answers (1)

Nix
Nix

Reputation: 5998

I solved the problem like so:

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

The XML is simple enough. It's simply <Page /> nodes nested into eachother.

<Page ID="99" MenuText="Branding days " Href="Default.aspx?ID=99" FriendlyHref="/en-GB/Corporate/Branding-days.aspx" ShowInSitemap="False" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" ChildCount="3" class="L2">

If anyone have a cleaner solution, let me know, but as for now it works as expected.

Upvotes: 1

Related Questions