XLST在web.sitemap中查找ASP.NET角色
我正在使用使用VS.net 2010和XSLT创建的web.sitemap创建一个干净的CSS菜单。XLST在web.sitemap中查找ASP.NET角色,asp.net,xslt,menu,sitemap,Asp.net,Xslt,Menu,Sitemap,我正在使用使用VS.net 2010和XSLT创建的web.sitemap创建一个干净的CSS菜单。 我已经从中修改了xslt,去掉了第一个节点,但是到目前为止,我还无法确定如何在中搜索,以便根据用户的角色仅显示链接 XSLT如下所示: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmln
我已经从中修改了xslt,去掉了第一个节点,但是到目前为止,我还无法确定如何在中搜索,以便根据用户的角色仅显示链接 XSLT如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" exclude-result-prefixes="map">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template name="mapNode" match="/">
<ul id="main-menu">
<xsl:apply-templates select="*"/>
</ul>
</xsl:template>
<xsl:template match="/*/*">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="map:siteMapNode">
<xsl:if test="/siteMap/SiteMapNode[@roles != 'Admin']">
<li>
<a href="{substring(@url, 2)}" title="{@description}">
<xsl:value-of select="@title"/>
</a>
<xsl:if test="map:siteMapNode">
<xsl:call-template name="mapNode"/>
</xsl:if>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
XML如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/" title="" description="Anon" roles="*">
<siteMapNode url="~/anon.aspx" title="Anon" description="Anon" roles="*" />
<siteMapNode url="~/admin1.aspx" title="Admin1" description="Admin Only" roles="Admin"/>
<siteMapNode url="~/admin2.aspx" title="Admin2" description="Admin Only" roles="Admin">
<siteMapNode url="~/admin3.aspx" title="Admin3" description="Admin Only" roles="Admin"/>
</siteMapNode>
</siteMapNode>
</siteMap>
我只想输出角色所在的URL标题和说明!=管理
没有搜索,一切正常。是否有人能够阐明“如果”功能,或提出更好的实现方法?
提前感谢您当前的xsl:if条件出现的问题
<xsl:if test="/siteMap/SiteMapNode[@roles != 'Admin']">
。。。。第一个正斜杠意味着它是一个绝对路径,从根元素开始,因此所有的xsl:if表示的是siteMap元素下是否有SiteMapNode,这不是管理员角色。这意味着在你的情况下它永远是真实的
您确实只想检查当前元素的角色
<xsl:if test="@roles != 'Admin'">
然而,有一种更整洁的方法可以做到这一点。删除xsl:if条件,只需使用一个单独的模板来匹配管理角色元素,并忽略它们
<xsl:template match="map:siteMapNode[@roles='Admin']"/>
这是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:map="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" exclude-result-prefixes="map">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template name="mapNode" match="/">
<ul id="main-menu">
<xsl:apply-templates select="*"/>
</ul>
</xsl:template>
<xsl:template match="/*/*">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="map:siteMapNode[@roles='Admin']"/>
<xsl:template match="map:siteMapNode">
<li>
<a href="{substring(@url, 2)}" title="{@description}">
<xsl:value-of select="@title"/>
</a>
<xsl:if test="map:siteMapNode">
<xsl:call-template name="mapNode"/>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
当应用于示例XML时,将输出以下内容
<ul id="main-menu">
<li>
<a href="/anon.aspx" title="Anon">Anon</a>
</li>
</ul>
-
请注意,匹配admin role元素的模板比匹配任何SiteMapNode元素的模板更具体,因此XSLT处理程序在匹配时会优先考虑此问题。您可以尝试
或更好的
谢谢Tim C和svz,您的两个解决方案都很划算。正如你所看到的,我仍然在思考这个问题,但当我无法在一天内找到解决方案时,有时问起来更容易。