【问题标题】:Umbraco XSLT. Dynamic menuUmbraco XSLT。动态菜单
【发布时间】:2013-11-07 15:12:23
【问题描述】:

我的 Umbraco 项目具有以下结构:

Content
-Home
--Contacts
--Offices
-About

首页有'/'地址,是一个索引页。 有人可以告诉我如何通过 XSLT 获取 Both - Home 和 About 元素以将它们放入菜单中吗? 目前我只能按当前页面的级别(即主页)获取子节点,所以我只得到联系人和办公室。我不能通过 documentType 来获取它们,因为我想要一个动态的,而不是硬编码的。 有没有办法让 Home 和 About 不断显示在菜单中? 我对 XSLT 完全陌生,而且是在 Umbraco 本身的第二天。

编辑:这是 XML 的一部分,在

<xsl:param name="currentPage"/>

<!-- Input the documenttype you want here -->
<xsl:variable name="level" select="1"/>

<xsl:template match="/">

<!-- The fun starts here -->
<ul>
<xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
    <xsl:if test="$currentPage/@id = @id">
        <li class="selected">
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>
        </li>
    </xsl:if>
    <xsl:if test="$currentPage/@id != @id">
        <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>
        </li>
    </xsl:if>
    <xsl:value-of select="$currentPage/id"/>
</xsl:for-each>
</ul>

</xsl:template>

Edit2:通过取消级别平等,我得到了“self”,即“Home”,但仍然没有“About”。 也许您知道如何查找有关“X-Path”选择器的信息,例如“ancestor-or-self::*”?

【问题讨论】:

  • 你能发布你想要转换的 XML
  • XML 标记已发布。希望它会有所帮助。
  • 我想更多的是您的源发送到 XSL 文件的原始 XML,而不是 XSL 本身。不过,在您提供的示例中,您可能会发现 xsl:choose 比 xsl:if 更好 - 请参阅 w3schools.com/xsl/xsl_choose.asp
  • 我知道 XSLT,但我根本不知道 Umbraco。在不真正了解 XML 源的样子的情况下使用 XSL 是我认为几乎不可能的事情,但是我已经查看了 Umbraco 文档,他们似乎建议您这样做。我已经编辑了您的问题,以展示一种更简单的方式来处理您的列表项,抱歉,我无法提供答案
  • 感谢您的评论,它有助于简化标记。

标签: xslt xpath umbraco


【解决方案1】:

我终于找到了答案。 通过项目根元素访问所有元素的方法是使用“$currentPage/ancestor::root//*”。 这就是我的标记现在的样子,在 John 的帮助下,简化了 'if' 语句。

<!-- Input the documenttype you want here -->
<xsl:variable name="level" select="1"/>

<xsl:template match="/">

<!-- The fun starts here -->
<ul>
    <xsl:for-each select="$currentPage/ancestor::root//*  [@level = $level and name() != 'Home' and @isDoc and string(umbracoNaviHide) != '1']">
        <li>
            <xsl:if test="$currentPage/@id = @id">
                <xsl:attribute name="class">selected</xsl:attribute>
            </xsl:if>
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
            </a>
        </li>
    <xsl:value-of select="$currentPage/id"/>
</xsl:for-each>
</ul>

在出口处,我得到了“内容”的所有元素,除了“主页”(我只是不只是在我的菜单中需要它)。

【讨论】:

  • 关于使用子菜单的任何想法,因为我的菜单很少有子菜单,这给我的只有父菜单而不是子菜单。
  • 你能提供更多关于这个的信息吗?也许你有一段代码?
  • 我得到了解决方案。感谢您浏览此处。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
相关资源
最近更新 更多