title: Give parent elements a class when children is active with XSLT
body: <p>Until recently XSLT was completely new to me, and I've been working on a menu/submenu in XSLT for a little while with a danish CMS called Dynamicweb.</p>
  
  <p>I don't know if this is a Dynamicweb specific question or a XSLT related question, but I'll ask anyway.</p>
  
  <p>My current XSLT document looks like this:</p>
  
  <pre><code>&lt;xsl:template match="//Page"&gt;
      &lt;xsl:param name="depth"/&gt;
      &lt;li&gt;
          &lt;xsl:attribute name="id"&gt;
              &lt;xsl:value-of select="concat('', translate(translate(@MenuText, translate(@MenuText, $validRange, ''), ''), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))" /&gt;
          &lt;/xsl:attribute&gt;
  
          &lt;a&gt;
              &lt;xsl:attribute name="class"&gt;
                  &lt;!-- Add .inpath class --&gt;
                  &lt;xsl:if test="@InPath='True'"&gt;
                      &lt;xsl:text&gt; inpath&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
  
                  &lt;!-- Add .firstitem class --&gt;
                  &lt;xsl:if test="position() = 1"&gt;
                      &lt;xsl:text&gt; firstitem&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
  
                  &lt;!-- Add .miditem class --&gt;
                  &lt;xsl:if test="position() &amp;gt; 1 and position() &amp;lt; count(//Page)"&gt;
                      &lt;xsl:text&gt; miditem&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
  
                  &lt;!-- Add .lastitem class --&gt;
                  &lt;xsl:if test="position() = count(//Page)"&gt;
                      &lt;xsl:text&gt; lastitem&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
  
                  &lt;!-- Add .active class --&gt;
                  &lt;xsl:if test="@Active = 'True'"&gt;
                      &lt;xsl:text&gt; active&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:attribute&gt;
  
              &lt;!-- Add link ID (URL friendly menu text) --&gt;
              &lt;xsl:attribute name="id"&gt;
                  &lt;xsl:value-of select="concat('anchor-', translate(translate(@MenuText, translate(@MenuText, $validRange, ''), ''), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))" /&gt;
              &lt;/xsl:attribute&gt;
  
              &lt;!-- Add link URL --&gt;
              &lt;xsl:attribute name="href"&gt;
                  &lt;xsl:value-of select="@FriendlyHref" disable-output-escaping="yes" /&gt;
              &lt;/xsl:attribute&gt;
  
              &lt;!-- Add link text --&gt;
              &lt;span&gt;&lt;xsl:value-of select="@MenuText" disable-output-escaping="yes" /&gt;&lt;/span&gt;
          &lt;/a&gt;
          &lt;xsl:if test="count(Page) and @MenuText != 'Home'"&gt;
              &lt;ul class="level{@AbsoluteLevel+1}"&gt;
                  &lt;xsl:apply-templates select="Page"&gt;
                      &lt;xsl:with-param name="depth" select="$depth+1"/&gt;
                  &lt;/xsl:apply-templates&gt;
              &lt;/ul&gt;
          &lt;/xsl:if&gt;
      &lt;/li&gt;
  
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>You can see how I add classes based on Dynamicweb tags for <code>active</code> and <code>inpath</code>. The thing is that further down in the document <strike>(not pasted here)</strike> is a code for printing out submenu (<code>ul</code> and <code>li</code>). If I click a submenu item, that item gets the <code>active</code> class, but is there a way to give the parent the <code>active</code> class too?</p>
  
  <p><strong>Update:</strong> Added the raw XML (sorry for the messy XML).</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;NavigationTree&gt;
    &lt;Settings&gt;
      &lt;Pageview ID="1" AreaID="1" MenuText="Home" Title="Home" NavigationName="" /&gt;
      &lt;Setting Level="1"&gt;
        &lt;NavigationImage Value="" /&gt;
        &lt;NavigationMouseoverImage Value="" /&gt;
        &lt;NavigationActiveImage Value="" /&gt;
        &lt;NavigationImgAfter Value="" /&gt;
        &lt;NavigationDividerImage Value="" /&gt;
        &lt;NavigationHideSpacer Value="True" /&gt;
        &lt;NavigationSpace Value="0" /&gt;
      &lt;/Setting&gt;
      &lt;Setting Level="2"&gt;
        &lt;NavigationImage Value="" /&gt;
        &lt;NavigationMouseoverImage Value="" /&gt;
        &lt;NavigationActiveImage Value="" /&gt;
        &lt;NavigationImgAfter Value="" /&gt;
        &lt;NavigationDividerImage Value="" /&gt;
        &lt;NavigationHideSpacer Value="" /&gt;
        &lt;NavigationSpace Value="0" /&gt;
      &lt;/Setting&gt;
      &lt;Setting Level="3"&gt;
        &lt;NavigationImage Value="" /&gt;
        &lt;NavigationMouseoverImage Value="" /&gt;
        &lt;NavigationActiveImage Value="" /&gt;
        &lt;NavigationImgAfter Value="" /&gt;
        &lt;NavigationDividerImage Value="" /&gt;
        &lt;NavigationHideSpacer Value="" /&gt;
        &lt;NavigationSpace Value="0" /&gt;
      &lt;/Setting&gt;
      &lt;Setting Level="4"&gt;
        &lt;NavigationImage Value="" /&gt;
        &lt;NavigationMouseoverImage Value="" /&gt;
        &lt;NavigationActiveImage Value="" /&gt;
        &lt;NavigationImgAfter Value="" /&gt;
        &lt;NavigationDividerImage Value="" /&gt;
        &lt;NavigationHideSpacer Value="" /&gt;
        &lt;NavigationSpace Value="3" /&gt;
      &lt;/Setting&gt;
      &lt;Setting Level="5"&gt;
        &lt;NavigationImage Value="" /&gt;
        &lt;NavigationMouseoverImage Value="" /&gt;
        &lt;NavigationActiveImage Value="" /&gt;
        &lt;NavigationImgAfter Value="" /&gt;
        &lt;NavigationDividerImage Value="" /&gt;
        &lt;NavigationHideSpacer Value="" /&gt;
        &lt;NavigationSpace Value="3" /&gt;
      &lt;/Setting&gt;
    &lt;/Settings&gt;
    &lt;Page ID="1" AreaID="1" MenuText="Home" MouseOver="" Href="Default.aspx?ID=1" FriendlyHref="/default/home.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="1" RelativeLevel="1" Sort="1" LastInLevel="False" InPath="True" ChildCount="3" class="L1_Active" Active="True" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"&gt;
      &lt;Page ID="6" AreaID="1" MenuText="News" MouseOver="" Href="Default.aspx?ID=6" FriendlyHref="/default/home/news.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="2" RelativeLevel="2" Sort="1" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
      &lt;Page ID="7" AreaID="1" MenuText="About" MouseOver="" Href="Default.aspx?ID=7" FriendlyHref="/default/home/about.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="2" RelativeLevel="2" Sort="2" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
      &lt;Page ID="8" AreaID="1" MenuText="Presence" MouseOver="" Href="Default.aspx?ID=8" FriendlyHref="/default/home/presence.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="2" RelativeLevel="2" Sort="3" LastInLevel="True" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
    &lt;/Page&gt;
    &lt;Page ID="2" AreaID="1" MenuText="Hygiene" MouseOver="" Href="Default.aspx?ID=14" FriendlyHref="/default/hygiene.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="False" ShowInSitemap="True" AbsoluteLevel="1" RelativeLevel="1" Sort="2" LastInLevel="False" InPath="False" ChildCount="2" class="L1" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"&gt;
      &lt;Page ID="14" AreaID="1" MenuText="Professional" MouseOver="" Href="Default.aspx?ID=14" FriendlyHref="/default/hygiene/professional.aspx" Image="/Files/Navigation/menu_antibac_01.gif" ImageActive="/Files/Navigation/menu_antibac_02.gif" ImageMouseOver="/Files/Navigation/menu_antibac_02.gif" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="2" RelativeLevel="2" Sort="1" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
      &lt;Page ID="15" AreaID="1" MenuText="Private" MouseOver="" Href="Default.aspx?ID=15" FriendlyHref="/default/hygiene/private.aspx" Image="/Files/Navigation/menu_antibac_01.gif" ImageActive="/Files/Navigation/menu_antibac_02.gif" ImageMouseOver="/Files/Navigation/menu_antibac_02.gif" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="2" RelativeLevel="2" Sort="2" LastInLevel="True" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
    &lt;/Page&gt;
    &lt;Page ID="3" AreaID="1" MenuText="Household &amp;amp; Leisure" MouseOver="" Href="Default.aspx?ID=3" FriendlyHref="/default/household---leisure.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="1" RelativeLevel="1" Sort="3" LastInLevel="False" InPath="False" ChildCount="0" class="L1" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
    &lt;Page ID="4" AreaID="1" MenuText="Car Care" MouseOver="" Href="Default.aspx?ID=4" FriendlyHref="/default/car-care.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="1" RelativeLevel="1" Sort="4" LastInLevel="False" InPath="False" ChildCount="1" class="L1" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"&gt;
      &lt;Page ID="20" AreaID="1" MenuText="Carix" MouseOver="" Href="Default.aspx?ID=20" FriendlyHref="/default/car-care/carix.aspx" Image="/Files/Navigation/menu_carix_01.gif" ImageActive="/Files/Navigation/menu_carix_02.gif" ImageMouseOver="/Files/Navigation/menu_carix_02.gif" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="2" RelativeLevel="2" Sort="1" LastInLevel="True" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
    &lt;/Page&gt;
    &lt;Page ID="5" AreaID="1" MenuText="Industrial Chemicals" MouseOver="" Href="Default.aspx?ID=5" FriendlyHref="/default/industrial-chemicals.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" AbsoluteLevel="1" RelativeLevel="1" Sort="5" LastInLevel="True" InPath="False" ChildCount="0" class="L1" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" /&gt;
  &lt;/NavigationTree&gt;
  </code></pre>