Click here to Skip to main content
12,897,242 members (56,034 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 27 Jan 2012

Using XSLT to Display an ASP.NET Sitemap Without Using Tables

, 27 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use XSLT to display an ASP.NET sitemap without using tables

The quick and easy way of displaying an ASP.NET site map (web.sitemap) in an ASP.NET page is to use a TreeView control bound to a SiteMapDataSource component as shown in the following example:

<asp:SiteMapDataSource runat="server" 

ID="siteMapDataSource" EnableViewState="False"   

ShowStartingNode="False" />
<asp:TreeView runat="server" ID="siteMapTreeView" 


EnableClientScript="False" EnableViewState="False" 


Which results in a mass of nested tables, in-line styles, and generally messy mark-up.

With just a little more effort however, you can display the sitemap using a XSLT transform, resulting in slim, clean and configurable mark-up - and not a table to be seen.

This approach can be used with both Web Forms and MVC.

This article assumes you already have a pre-made ASP.NET sitemap file.

Defining the XSLT

Add a new XSLT File to your project. In this case, it's named sitemap.xslt.

Next, paste in the mark-up below.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="" 


  <xsl:output method="xml" 

  encoding="utf-8" indent="yes"/>

  <xsl:template name="mapNode" match="map:siteMap">

  <xsl:template match="map:siteMapNode">
      <a href="{substring(@url, 2)}" 

        <xsl:value-of select="@title"/>

      <xsl:if test="map:siteMapNode">
        <xsl:call-template name="mapNode"/>


Note: As generally all URLs in ASP.NET site maps start with ~/, the href tag in the above example has been customized to include the domain at the start, then use the XSLT substring function to strip the ~/ from the start of the URL. Don't forget to modify the URL to point to your own domain!

Declaratively Transforming the Document

If you are using Web forms controls, then this may be the more convenient approach for you.

Just add the XML component to your page, and set the DocumentSource property to the name of the sitemap, and the TransformSource property to the name of your XSLT file.

<asp:Xml runat="server" ID="xmlSiteMapViewer" DocumentSource="~/web.sitemap" 

TransformSource="~/sitemap.xslt" />

Programmatically Transforming the Document

The ASP.NET XML control doesn't need to be inside a server side form tag, so you can use the exact same code above in your MVC views.

However, if you want to do this programmatically, the following code works too.

var xmlFileName = Server.MapPath("~/web.sitemap");
var xslFileName=Server.MapPath("~/sitemap.xslt");
var result =new System.IO.StringWriter();
var transform = new System.Xml.Xsl.XslCompiledTransform();

transform.Transform(xmlFileName, null, result);


The Result

The output of the transform will be a simple series of nested unordered lists, clean and ready to be styled with CSS. And for little more effort than it took to do the original tree view solution.

With a bit more tweaking, you can probably expand this to show only a single branch, useful for navigation within a section of a website, or creating breadcrumb trails.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Richard James Moss
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

BugFault basic the breadcrumb Pin
Luiz Baggio6-Nov-13 14:10
memberLuiz Baggio6-Nov-13 14:10 
GeneralRe: Fault basic the breadcrumb Pin
Richard James Moss7-Nov-13 2:38
memberRichard James Moss7-Nov-13 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 27 Jan 2012
Article Copyright 2012 by Richard James Moss
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid