- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
直到最近,XSLT 对我来说还是全新的,而且我一直在使用名为 Dynamicweb 的丹麦 CMS 使用 XSLT 中的菜单/子菜单工作一段时间。
我不知道这是 Dynamicweb 特定问题还是 XSLT 相关问题,但无论如何我都会问。
我当前的 XSLT 文档如下所示:
<xsl:template match="//Page">
<xsl:param name="depth"/>
<li>
<xsl:attribute name="id">
<xsl:value-of select="concat('', translate(translate(@MenuText, translate(@MenuText, $validRange, ''), ''), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))" />
</xsl:attribute>
<a>
<xsl:attribute name="class">
<!-- Add .inpath class -->
<xsl:if test="@InPath='True'">
<xsl:text> inpath</xsl:text>
</xsl:if>
<!-- Add .firstitem class -->
<xsl:if test="position() = 1">
<xsl:text> firstitem</xsl:text>
</xsl:if>
<!-- Add .miditem class -->
<xsl:if test="position() > 1 and position() < count(//Page)">
<xsl:text> miditem</xsl:text>
</xsl:if>
<!-- Add .lastitem class -->
<xsl:if test="position() = count(//Page)">
<xsl:text> lastitem</xsl:text>
</xsl:if>
<!-- Add .active class -->
<xsl:if test="@Active = 'True'">
<xsl:text> active</xsl:text>
</xsl:if>
</xsl:attribute>
<!-- Add link ID (URL friendly menu text) -->
<xsl:attribute name="id">
<xsl:value-of select="concat('anchor-', translate(translate(@MenuText, translate(@MenuText, $validRange, ''), ''), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))" />
</xsl:attribute>
<!-- Add link URL -->
<xsl:attribute name="href">
<xsl:value-of select="@FriendlyHref" disable-output-escaping="yes" />
</xsl:attribute>
<!-- Add link text -->
<span><xsl:value-of select="@MenuText" disable-output-escaping="yes" /></span>
</a>
<xsl:if test="count(Page) and @MenuText != 'Home'">
<ul class="level{@AbsoluteLevel+1}">
<xsl:apply-templates select="Page">
<xsl:with-param name="depth" select="$depth+1"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</li>
</xsl:template>
您可以看到我如何根据 active
和 inpath
的 Dynamicweb 标签添加类。问题是在文档
(此处未粘贴)
的下方是用于打印子菜单的代码(ul
和 li
)。如果我单击一个子菜单项,该项目将获得 active
类,但是有没有办法也为父级提供 active
类?
更新:添加了原始 XML(对困惑的 XML 感到抱歉)。
<?xml version="1.0" encoding="utf-8"?>
<NavigationTree>
<Settings>
<Pageview ID="1" AreaID="1" MenuText="Home" Title="Home" NavigationName="" />
<Setting Level="1">
<NavigationImage Value="" />
<NavigationMouseoverImage Value="" />
<NavigationActiveImage Value="" />
<NavigationImgAfter Value="" />
<NavigationDividerImage Value="" />
<NavigationHideSpacer Value="True" />
<NavigationSpace Value="0" />
</Setting>
<Setting Level="2">
<NavigationImage Value="" />
<NavigationMouseoverImage Value="" />
<NavigationActiveImage Value="" />
<NavigationImgAfter Value="" />
<NavigationDividerImage Value="" />
<NavigationHideSpacer Value="" />
<NavigationSpace Value="0" />
</Setting>
<Setting Level="3">
<NavigationImage Value="" />
<NavigationMouseoverImage Value="" />
<NavigationActiveImage Value="" />
<NavigationImgAfter Value="" />
<NavigationDividerImage Value="" />
<NavigationHideSpacer Value="" />
<NavigationSpace Value="0" />
</Setting>
<Setting Level="4">
<NavigationImage Value="" />
<NavigationMouseoverImage Value="" />
<NavigationActiveImage Value="" />
<NavigationImgAfter Value="" />
<NavigationDividerImage Value="" />
<NavigationHideSpacer Value="" />
<NavigationSpace Value="3" />
</Setting>
<Setting Level="5">
<NavigationImage Value="" />
<NavigationMouseoverImage Value="" />
<NavigationActiveImage Value="" />
<NavigationImgAfter Value="" />
<NavigationDividerImage Value="" />
<NavigationHideSpacer Value="" />
<NavigationSpace Value="3" />
</Setting>
</Settings>
<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">
<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" />
<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" />
<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" />
</Page>
<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">
<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" />
<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" />
</Page>
<Page ID="3" AreaID="1" MenuText="Household & 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" />
<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">
<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" />
</Page>
<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" />
</NavigationTree>
最佳答案
好吧,一些观察结果:
您不需要 match='//Page'
中的前导斜杠。 match
属性中的 XPath 用于选择模板,而不是导航到节点。
您不需要depth
参数;因为 Page
的深度是它拥有的 Page
祖先的数量,你可以只使用 count(ancestor::Page)
来计算深度。
如果您想通过遍历 Page
元素树来获取 Active
属性,请使用 ancestor-or-self::Page[ last()]/@Active
. ancestor-or-self 轴是一个节点列表,从上下文节点开始,包括它的父节点、它的父节点的父节点等等,直到到达 TLE。 Page
部分仅在该轴上找到 Page
元素,而 [last()]
谓词找到那些 Page< 中的最后一个
元素,它始终是最高级别的 Page
元素。
我强烈怀疑您对 count(//Page)
的使用从长远来看不会很好地为您服务。这会计算源代码树中的每个 Page
元素,无论它位于何处。那真的是你想要的吗?或者您想知道当前 Page
元素相对于其同级 Page
元素的位置吗?如果是这样,您只需执行 position() != 1 和 position() != last()
。这是有效的,因为 position()
返回上下文节点相对于其表达式上下文的位置。也就是说,当调用 xsl:apply-templates select='*'
时,它会创建一组元素并为每个元素找到匹配的模板。那是表达语境;该列表中的第三个元素的 position()
为 3。
无需执行 test='count(Page)
。 test='Page'
做同样的事情;如果有任何子 Page
元素,它的计算结果为 true。它更具可读性,而且很可能更快。
编辑
结合了 Tomalak 的观察(见评论)。
编辑
你知道,我实际上应该阅读这些问题。在您的上下文中,要在父元素或其子元素之一 处于事件状态时为父元素赋予一个类,与我所做的相反,您可以这样做:
<xsl:if test="@Active='True' or Page[@Active = 'True']">
<xsl:text> active</xsl:text>
</xsl:if>
但是 Tomalak 的示例使用 descendant-or-self
轴,如果您的想法是在菜单中的任何位置处于事件状态时激活菜单,则它更可能是您想要使用的。你也可以这样做:
<xsl:if test="@Active='True' or .//Page[@Active = 'True']">
<xsl:text> active</xsl:text>
</xsl:if>
因为“.//”实际上只是descendant::
的快捷方式。
关于xml - 当子元素使用 XSLT 激活时,给父元素一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2107890/
我有一个 XSLT 样式表,如下所示: 我想使用第二个 XSLT 样式表来转换此样式表,以删除与 XQHead
我们有一个大型 xslt,可以呈现整个商店区域,包括产品、制造商,并根据价格和类别进行过滤。我使用 sitecore 作为 CMS,但遇到缓存问题。我有大约 9000 个项目,有些页面需要长达 20
我想根据条件的结果应用具有不同参数的模板。像这样: Attribute no. 1
我有一些看起来像这样的 XML Foo Details Bar Details Baz Details Foo Blah Bar BlahBlah Baz BlahBlahBl
我试图从这种输入出发: a b c d e f g ... 使用 XSLT 的 HTML 输出: one two a e b f
我想知道如何在 xslt 中找到特定节点的第一个子节点名称。 我有一个 xml: some text 我可以使用 body/
是否可以在 XSLT 中获取上个月的最后一天?我找到了这个函数:http://www.xsltfunctions.com/xsl/functx_last-day-of-month.html但我不确定如
具有特定节点的匹配元素存在问题。 xml: description of profile PhoneKeyPad S
我将一堆键值对作为参数传递给 XSL(日期 ->“1 月 20 日”,作者 ->“Dominic Rodger”,...)。 我正在解析的一些 XML 中引用了这些 - XML 如下所示: 目前,除
我找不到这个问题的确切答案,所以我希望有人能在这里帮助我。 我有一个字符串,我想在最后一个 '.' 之后获取子字符串。我正在使用 xslt 1.0。 这是怎么做的?这是我的代码。
我在尝试找出 xslt 上的 var 范围时遇到问题。我实际上想要做的是忽略具有重复“旅游代码”的“旅行”标签。 示例 XML: X1 Budapest X1 Budapest X
我有一些数据在 xslt 的 for-each 循环中输出。我对列表进行了分页,但没有对排序选择器进行分页。 用户应该能够对 2 个值(创建的数据和每个项目的数字字段)进行排序。默认的排序方法是创建日
我有一个奇怪的要求。 我在 xslt 中有一个包含月份的变量,带有它们的 id (1-12) 问题是我需要全部显示它们,但从一月(1)以外的月份开始。 目前我有以下 JAN
如何在 xslt 转换中模块化一组重复的输出?例如,我有如下内容(伪代码)。 并
我得到一个像这样的字符串。 13091711111100222222003333330044444400 字符串的模式是这样的 13 - 09 - 17 - 11111 - 100 - 22222 -
我是 XSLT 的新手,有一个一般性问题。为了区分具有不同属性的两个元素,最好(也是为了性能)使用 和 而不是 在一个模板中。据我所知,这就是 XSLT 中应该“思考”的方式。但在我看来,这有一个缺点
如何从“19650512-0065”到“196505120065”这样的字符串中删除连字符 使用这个模板:传递 theID =
是否有任何功能可以在左侧填充零? 我正在尝试做的要求是: 我们不知道即将到来的输入字符串长度。 如果小于 20,我们必须在左侧填充零。 如果输入字符串长度为 10,那么我们必须在左侧填充 10 个零。
身份模板如下所示: 是否选择多于 ,或者身份模板可能是这样的? 当我执行以下操作时,究竟选择了什么? 最佳答案
我正在尝试使用 XML 信息和 XSLT 模板创建超链接。这是 XML 源代码。 Among individual stocks, the top percentage gainers in the
我是一名优秀的程序员,十分优秀!