【问题标题】:xslt navigation apply template to child nodesxslt 导航将模板应用于子节点
【发布时间】:2016-07-18 07:43:38
【问题描述】:

我有一个这样的 XML 文档

<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 中得到一个这样的结果

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

基本上我不确定如何将每个 2 级项目包装在 div 和 ul 元素中,我也在寻找一种递归方法,以便它也可以处理 2 级以下的 3 级。任何帮助都会很棒!

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    在您的 XSLT 中,您调用了一个名为 MegaMenu 的命名模板,该模板在别处未显示。我不确定这是要做什么,但我认为要解决您当前的问题,您可以使用子页面的 xsl:for-each,在其中创建 div 标记,然后递归调用“页面”模板.

    试试这个 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>
    

    【讨论】:

    • 我昨天按照您建议的方式解决了我的问题 :)
    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2020-12-12
    • 2023-04-06
    • 2017-12-07
    相关资源
    最近更新 更多