2016-03-30 98 views
0

我有這樣XSLT導航應用模板子節點

<Page ID="28" AreaID="1" MenuText="Om IAI" Href="Default.aspx?ID=28" FriendlyHref="/da-dk/om-iai.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="1" RelativeLevel="1" Sort="1" LastInLevel="False" InPath="True" ChildCount="2" class="L1_Active" Active="True" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"> 
    <Page ID="29" AreaID="1" MenuText="Underside med langt navn" Href="Default.aspx?ID=29" FriendlyHref="/da-dk/om-iai/underside-med-langt-navn.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="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" /> 
    <Page ID="30" AreaID="1" MenuText="SubPage 2" Href="Default.aspx?ID=30" FriendlyHref="/da-dk/om-iai/subpage-2.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="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" /> 
    </Page> 

一個XML文檔,我想出來這樣從上面的XML

<ul> 
    <li> 
    <a>level 1</a> 
     <nav> 
      <div><ul><li><a>level 2</a></li></ul></div> 
      <div><ul><li><a>level 2</a></li></ul></div> 
     </nav> 
    </li> 
</ul> 

我的XSLT模板看起來像這樣

<xsl:template match="//Page"> 
     <xsl:param name="depth"/> 
    <xsl:copy-of select="."/> 
     <li> 
      <xsl:attribute name="class"> 
       <xsl:if test="@ChildCount > 0">dropdown </xsl:if> 
      </xsl:attribute> 
     <xsl:choose> 
      <xsl:when test="@ChildCount > 0"> 
      <a> 
         <xsl:attribute name="href"> 
        <xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/> 
         </xsl:attribute> 
       <xsl:attribute name="class"> 
        <xsl:text>dropdown-toggle</xsl:text>    
       </xsl:attribute> 
       <xsl:attribute name="data-toggle"> 
        <xsl:text>dropdown</xsl:text> 
       </xsl:attribute> 
         <xsl:value-of select="@MenuText" disable-output-escaping="yes"/> 
        </a> 
      </xsl:when> 
      <xsl:otherwise> 
      <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:otherwise> 
     </xsl:choose> 
     <xsl:if test="count(Page)"> 
      <nav class="dropdown-menu col-sm-12"> 
      <xsl:call-template name="MegaMenu"> 
       <xsl:with-param name="depth" select="$depth+1"/> 
      </xsl:call-template> 
     </nav> 
     </xsl:if> 
     </li> 
    </xsl:template> 

基本上我不確定如何在div和ul元素中包裝每個級別2項目,我也在尋找遞歸e方法,所以它也可以處理2級以下的級別3.任何幫助都會很棒!

回答

1

在您的XSLT中,您調用名爲MegaMenu的命名模板,該模板未在別處顯示。我不確定這是什麼意思,但我認爲要解決您當前的問題,您可以使用子頁面的xsl:for-each,在那裏創建div標記,然後遞歸調用「Page」模板。

試試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:template match="Page"> 
     <xsl:param name="depth" select="1"/> 
     <ul> 
      <li> 
       <a href="{@FriendlyHref}"> 
        <xsl:text>Level </xsl:text> 
        <xsl:value-of select="$depth" /> 
        <xsl:text> - </xsl:text> 
        <xsl:value-of select="@MenuText" disable-output-escaping="yes"/> 
       </a> 
       <xsl:if test="Page"> 
        <nav> 
         <xsl:for-each select="Page"> 
          <div> 
           <xsl:apply-templates select="."> 
            <xsl:with-param name="depth" select="$depth + 1" /> 
           </xsl:apply-templates> 
          </div> 
         </xsl:for-each> 
        </nav> 
       </xsl:if> 
      </li> 
     </ul> 
    </xsl:template> 
</xsl:stylesheet> 
+0

昨天我解決我的問題,正好你提出同樣的方式:) –