【问题标题】:XLST to find ASP.NET roles in web.sitemapXSLT 在 web.sitemap 中查找 ASP.NET 角色
【发布时间】:2012-09-24 05:33:42
【问题描述】:

我正在使用使用 VS.net 2010 创建的 web.sitemap 以及 XSLT 来创建一个干净的 CSS 菜单。
我已经从 Cyotec 修改了 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>

我只想输出角色 != Admin 的 url 标题和描述 没有搜索一切正常。
有没有人能够阐明“if”功能,或者提出更好的方法来实现这一点?
提前致谢

【问题讨论】:

  • 你可以试试&lt;xsl:if test="not(/siteMap/SiteMapNode/@roles = 'Admin')"&gt;或者更好的&lt;xsl:temlate match="map:siteMapNode[not(@role = 'Admin')]"&gt;

标签: asp.net xslt menu sitemap


【解决方案1】:

您当前 xsl:if 条件的问题......

<xsl:if test="/siteMap/SiteMapNode[@roles != 'Admin']"> 

.... 是第一个正斜杠表示它是绝对路径,从根元素开始,所以所有 xsl:if 都在说是否有任何 SiteMapNode ,直接在 siteMap 元素下,不是管理员角色。这意味着它在你的情况下永远是正确的。

你真的只想检查当前元素的角色

<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 角色元素的模板比匹配任何 SiteMapNode 元素的模板更具体,因此 XSLT 处理器在匹配时会优先考虑这一点。

【讨论】:

  • 感谢 Tim C 和 svz,您的两个解决方案都非常划算。如您所见,我仍在思考这个问题,但是当我无法在一天内找到解决方案时,有时问起来会更容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 2012-10-16
  • 1970-01-01
相关资源
最近更新 更多