Xml DotNetNuke+;XPath=自定义导航菜单
我正在使用菜单组件开发DotNetNuke 5的皮肤,该组件使用自定义XSL-T将XML站点地图转换为HTML导航 与构建导航菜单的其他方法相比,使用XSL-T是一个救生圈。但是,由于我是XML-T和XPath的新手,在做一些稍微复杂一点的事情时遇到了问题 我想要实现的是:Xml DotNetNuke+;XPath=自定义导航菜单,xml,xslt,xpath,dotnetnuke,Xml,Xslt,Xpath,Dotnetnuke,我正在使用菜单组件开发DotNetNuke 5的皮肤,该组件使用自定义XSL-T将XML站点地图转换为HTML导航 与构建导航菜单的其他方法相比,使用XSL-T是一个救生圈。但是,由于我是XML-T和XPath的新手,在做一些稍微复杂一点的事情时遇到了问题 我想要实现的是: 忽略级别0节点 循环通过选定层次结构中的所有级别1节点 找到选定的级别1节点后,开始在层次结构/面包屑结构中循环,直至活动级别 进入活动节点级别后,循环并显示该级别的所有同级 然后完成通过级别1节点的循环 下面是菜单组件
- 忽略级别0节点
- 循环通过选定层次结构中的所有级别1节点
- 找到选定的级别1节点后,开始在层次结构/面包屑结构中循环,直至活动级别
- 进入活动节点级别后,循环并显示该级别的所有同级
- 然后完成通过级别1节点的循环
<Root>
<root>
<node id="37" text="Home" url="http://www.dnndoneright.com/Home.T37.aspx" enabled="1" selected="0" breadcrumb="0" first="1" last="0" only="0" depth="0" >
<description >Upgrade your standard DNN menu - automatically and for free - to an SEO-optimised, accessible, mobile-friendly, cross-browser menu.</description>
</node>
<node id="56" text="DNN Menu" url="http://www.dnndoneright.com/DNN-Menu.T56.aspx" enabled="1" selected="0" breadcrumb="1" first="0" last="0" only="0" depth="0" >
<node id="97" text="Menu features" url="http://www.dnndoneright.com/Menu-features.T97.aspx" enabled="1" selected="0" breadcrumb="0" first="1" last="0" only="0" depth="1" />
<node id="111" text="Pre-built templates" url="http://www.dnndoneright.com/Pre-built-templates.T111.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="1" >
<node id="94" text="Standard DNN menu, upgraded" url="http://www.dnndoneright.com/Standard-DNN-menu-upgraded.T94.aspx" enabled="1" selected="0" breadcrumb="0" first="1" last="0" only="0" depth="2" />
<node id="124" text="Superfish template" url="http://www.dnndoneright.com/Superfish-template.T124.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="106" text="Treeview template" url="http://www.dnndoneright.com/Treeview-template.T106.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="107" text="Accordion template" url="http://www.dnndoneright.com/Accordion-template.T107.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="112" text="Dropdown template" url="http://www.dnndoneright.com/Dropdown-template.T112.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="96" text="Mega-menu" url="http://www.dnndoneright.com/Mega-menu.T96.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="1" only="0" depth="2" />
</node>
<node id="113" text="Getting started" url="http://www.dnndoneright.com/Getting-started.T113.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="1" >
<node id="114" text="Installing the menu" url="http://www.dnndoneright.com/Installing-the-menu.T114.aspx" enabled="1" selected="0" breadcrumb="0" first="1" last="0" only="0" depth="2" />
<node id="115" text="Upgrading a SolPart menu" url="http://www.dnndoneright.com/Upgrading-a-SolPart-menu.T115.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="116" text="Upgrading a DNNMenu" url="http://www.dnndoneright.com/Upgrading-a-DNNMenu.T116.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="117" text="Installing a template" url="http://www.dnndoneright.com/Installing-a-template.T117.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="118" text="Adding to a skin" url="http://www.dnndoneright.com/Adding-to-a-skin.T118.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="1" only="0" depth="2" />
</node>
<node id="119" text="Customising templates" url="http://www.dnndoneright.com/Customising-templates.T119.aspx" enabled="0" selected="0" breadcrumb="1" first="0" last="1" only="0" depth="1" >
<node id="120" text="Standard options" url="http://www.dnndoneright.com/Standard-options.T120.aspx" enabled="1" selected="0" breadcrumb="0" first="1" last="0" only="0" depth="2" />
<node id="121" text="Specifying filenames" url="http://www.dnndoneright.com/Specifying-filenames.T121.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="2" />
<node id="122" text="XML format" url="http://www.dnndoneright.com/XML-format.T122.aspx" enabled="1" selected="1" breadcrumb="1" first="0" last="0" only="0" depth="2" />
<node id="123" text="Creating custom templates" url="http://www.dnndoneright.com/Creating-custom-templates.T123.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="1" only="0" depth="2" />
</node>
</node>
<node id="87" text="Download" url="http://www.dnndoneright.com/Download.T87.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="0" only="0" depth="0" />
<node id="85" text="Contact" url="http://www.dnndoneright.com/Contact.T85.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="1" only="0" depth="0" >
<node id="129" text="Blog" url="http://www.dnndoneright.com/Blog.T129.aspx" enabled="1" selected="0" breadcrumb="0" first="1" last="0" only="0" depth="1" />
<node id="130" text="Forum" url="http://www.dnndoneright.com/Forum.T130.aspx" enabled="1" selected="0" breadcrumb="0" first="0" last="1" only="0" depth="1" />
</node>
</root>
</Root>
升级您的标准DNN菜单-自动和免费-到搜索引擎优化优化,可访问,移动友好,跨浏览器菜单。
这里也是我(不正确)的出发点,只是为了暴露我对XSL/XPath知识的缺乏。(我的代码是错误的,因为它列出了所有L1节点,而不仅仅是选定层次结构中的L1节点)
[ : ]
以下假设输入中只能有一个选定节点:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="/">
<div class="SideMenu">
<!-- select all the siblings of the selected node -->
<xsl:apply-templates select="//node[@selected=1]/../node" />
</div>
</xsl:template>
<!-- nodes are turned into a <div> with some descriptive text -->
<xsl:template match="node">
<div>
<xsl:value-of select="concat('[ ', @text, ': ', ' ]')" />
</div>
</xsl:template>
</xsl:stylesheet>
示例XML的输出:
<div class="SideMenu">
<div>[ Standard options: ]</div>
<div>[ Specifying filenames: ]</div>
<div>[ XML format: ]</div>
<div>[ Creating custom templates: ]</div>
</div>
[标准选项:]
[指定文件名:]
[XML格式:]
[创建自定义模板:]
嵌套菜单很容易创建,但无可否认,我并不确定您想要什么。如果您发布了所需的输出,我可以看到。如果您可以发布您希望看到的实际输出,那么它会有所帮助,但我认为您可能需要这样的内容:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<div class="SideMenu">
<!-- Loop through level 1 nodes -->
<xsl:apply-templates select="//node[@depth=1]" mode="general" />
</div>
</xsl:template>
<xsl:template match="node" mode="general">
<xsl:choose>
<!-- If the currently selected node, show all siblings -->
<xsl:when test="@selected=1">
<xsl:apply-templates select="../node" mode="only" />
</xsl:when>
<!-- Otherwise show just this node -->
<xsl:otherwise>
<xsl:apply-templates select="." mode="only" />
</xsl:otherwise>
</xsl:choose>
<!-- Follow the breadcrumb down towards the currently selected node -->
<xsl:apply-templates select="node[@breadcrumb=1]" mode="general" />
</xsl:template>
<xsl:template match="node" mode="only">
<div>
[ <xsl:value-of select="@text"/>: <xsl:value-of select="@depth"/> ]
</div>
</xsl:template>
</xsl:stylesheet>
[ : ]
如果这还不能满足你的需求,请在DNNdonerlight论坛上随意提问,这就是他们的目的 谢谢你们两位的回复。不幸的是,您的代码并不是我所需要的(很可能是由于我所追求的内容的复杂性以及我无法解释我的需求),但由于您的示例代码,我对XSL-t有了更深的理解,并创建了以下内容:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<div class="SideMenu">
<!-- Loop through all L1 nodes -->
<xsl:for-each select="Root/root/node">
<xsl:if test="@enabled='1'" >
<xsl:if test="@breadcrumb='1'" >
<!-- Find active root node-->
<!-- L0 nodes: Don't ever show (These are the horizontal tabs) -->
<!-- Always show all children: L1 nodes -->
<xsl:apply-templates select="child::node()[@url]" mode="BaseNode" ></xsl:apply-templates>
</xsl:if>
</xsl:if>
</xsl:for-each>
</div>
</xsl:template>
<!-- Template: Base node -->
<xsl:template match="node" mode="BaseNode" >
<xsl:choose>
<xsl:when test="@breadcrumb='0'" >
<!-- Non-breadcrumb L1 node -->
<xsl:apply-templates select="." mode="NonShadedNode" ></xsl:apply-templates>
</xsl:when>
<xsl:when test="@breadcrumb='1'" >
<!-- Child which is part of hierarchy -->
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
<!-- Drill down through hierarchy of active nodes-->
<xsl:for-each select="descendant::node()[@url]">
<xsl:if test="@breadcrumb='1'" >
<!-- Is this the last node? If so, show children -->
<xsl:choose>
<xsl:when test="child::node()[@url]">
<!-- Not last node -->
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<!-- Last node: loop through all siblings -->
<!-- THIS LOOP DOESN'T WORK CORRECTLY -->
<xsl:for-each select="preceding-sibling::node()" >
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
</xsl:for-each>
<xsl:apply-templates select="." mode="SelectedNode" ></xsl:apply-templates>
<xsl:for-each select="following-sibling::node()" >
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- Template: Non-shaded node -->
<xsl:template match="node" mode="NonShadedNode" >
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="style">
padding-left:<xsl:value-of select="@depth * 10"/>
</xsl:attribute>
<xsl:value-of select="@text"/>
</a>
</xsl:template>
<!-- Template: Shaded node -->
<xsl:template match="node" mode="ShadedNode" >
<a class="SideMenu_Shaded">
<xsl:attribute name="href">
<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="style">
padding-left:<xsl:value-of select="@depth * 10"/>
</xsl:attribute>
<xsl:value-of select="@text"/>
</a>
</xsl:template>
<!-- Template: Selected node -->
<xsl:template match="node" mode="SelectedNode" >
<a class="SideMenu_Selected">
<xsl:attribute name="style">
padding-left:<xsl:value-of select="@depth * 10"/>
</xsl:attribute>
<xsl:value-of select="@text"/>
</a>
</xsl:template>
</xsl:stylesheet>
警长专区
警长专区
提示:请不要将XML声明与XML代码示例一起发布。声明中的编码(ISO-8859-1)和实际编码(UTF-8,与网页的其余部分一样)不同。需要删除XML声明
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<div class="SideMenu">
<!-- Loop through all L1 nodes -->
<xsl:for-each select="Root/root/node">
<xsl:if test="@enabled='1'" >
<xsl:if test="@breadcrumb='1'" >
<!-- Find active root node-->
<!-- L0 nodes: Don't ever show (These are the horizontal tabs) -->
<!-- Always show all children: L1 nodes -->
<xsl:apply-templates select="child::node()[@url]" mode="BaseNode" ></xsl:apply-templates>
</xsl:if>
</xsl:if>
</xsl:for-each>
</div>
</xsl:template>
<!-- Template: Base node -->
<xsl:template match="node" mode="BaseNode" >
<xsl:choose>
<xsl:when test="@breadcrumb='0'" >
<!-- Non-breadcrumb L1 node -->
<xsl:apply-templates select="." mode="NonShadedNode" ></xsl:apply-templates>
</xsl:when>
<xsl:when test="@breadcrumb='1'" >
<!-- Child which is part of hierarchy -->
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
<!-- Drill down through hierarchy of active nodes-->
<xsl:for-each select="descendant::node()[@url]">
<xsl:if test="@breadcrumb='1'" >
<!-- Is this the last node? If so, show children -->
<xsl:choose>
<xsl:when test="child::node()[@url]">
<!-- Not last node -->
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<!-- Last node: loop through all siblings -->
<!-- THIS LOOP DOESN'T WORK CORRECTLY -->
<xsl:for-each select="preceding-sibling::node()" >
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
</xsl:for-each>
<xsl:apply-templates select="." mode="SelectedNode" ></xsl:apply-templates>
<xsl:for-each select="following-sibling::node()" >
<xsl:apply-templates select="." mode="ShadedNode" ></xsl:apply-templates>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- Template: Non-shaded node -->
<xsl:template match="node" mode="NonShadedNode" >
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="style">
padding-left:<xsl:value-of select="@depth * 10"/>
</xsl:attribute>
<xsl:value-of select="@text"/>
</a>
</xsl:template>
<!-- Template: Shaded node -->
<xsl:template match="node" mode="ShadedNode" >
<a class="SideMenu_Shaded">
<xsl:attribute name="href">
<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:attribute name="style">
padding-left:<xsl:value-of select="@depth * 10"/>
</xsl:attribute>
<xsl:value-of select="@text"/>
</a>
</xsl:template>
<!-- Template: Selected node -->
<xsl:template match="node" mode="SelectedNode" >
<a class="SideMenu_Selected">
<xsl:attribute name="style">
padding-left:<xsl:value-of select="@depth * 10"/>
</xsl:attribute>
<xsl:value-of select="@text"/>
</a>
</xsl:template>
</xsl:stylesheet>
<div class="SideMenu"><a href="/Home/Tester.aspx" style="
 padding-left:10">Tester </a><a class="SideMenu_Shaded" href="/Home/ChiefConstablesArea.aspx" style="
 padding-left:10">Chief Constables Area</a>
Chief Constables Area
Chief Constables Area
<a class="SideMenu_Shaded" href="/Home/ChiefConstablesArea/ChiefsFocusGroups.aspx" style="
 padding-left:20">Chief's Focus Groups</a>
<a class="SideMenu_Shaded" href="/Home/ChiefConstablesArea/AbouttheChief.aspx" style="
 padding-left:20">About the Chief</a>
<a class="SideMenu_Shaded" href="/Home/ChiefConstablesArea/ChiefsOfficerGroup.aspx" style="
 padding-left:20">Chief's Officer Group</a>
<a class="SideMenu_Selected" style="
 padding-left:20">Chief's Messages</a>
<a class="SideMenu_Shaded" href="/Home/ChiefConstablesArea/ForceAwardsScheme.aspx" style="
 padding-left:20">Force Awards Scheme</a>
<a href="/Home/ActiveForumLitetrial.aspx" style="
 padding-left:10">Active Forum Lite trial</a></div>