Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 1st, 2007, 11:27 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calling Web.Sitemap from XSLT

I have a site that uses the Master Page method in ASP.NET, and it contains a Web.sitemap doc that contains all of the navigation for the site. The Web.sitemap doc is basically an XML doc, so I was hoping that I could pull data from it using the XSLT document method, like this:

Web.sitemap:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="~/default.aspx" title="Home Page" id="default">
    <siteMapNode url="~/aboutus/aboutus.aspx" title="About Us" description="This page contains basic information about us." id="aboutus" />
  </siteMapNode>
</siteMap>
XSLT doc:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:variable name="websitemap" select="document('http://localhost:1309/phase3/Web.sitemap')" />
    <xsl:template match="/">
        Link: <xsl:value-of select="$websitemap/siteMap/siteMapNode/siteMapNode[@id='aboutus']" /><br />
    </xsl:template>
</xsl:stylesheet>
...but it's not working. Is this setup possible? If so, what am I doing wrong? Thanks for any help.

KWilliams
  #2 (permalink)  
Old March 1st, 2007, 11:32 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Read the first post in this forum and follow the link regarding namespaces.

--

Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old March 1st, 2007, 11:38 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I appreciate you pointing me in the right direction, but could you please help me to know which of the 58 sections I need to focus on? I've searched for the keywords "ASP.NET" and "sitemap" on that page, but nothing came up. I'll keep looking, but I'd appreciate any help.

KWilliams
  #4 (permalink)  
Old March 1st, 2007, 12:50 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, the links aren't working how they should, you need number 23, matching with a default namespace.

--

Joe (Microsoft MVP - XML)
  #5 (permalink)  
Old March 1st, 2007, 01:22 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for pointing that out. I reviewed the section, and applied it to my page with some success. I'm on the "About Us" page, and I want to create a link to the "Area Links" page. But my setup is only pulling some of the attributes for the "About Us" page from the Web.sitemap doc, whether or not I try to specify another id attribute. This is how I have the XSLT doc set up:

XSLT DOC:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sitemap="http://localhost:1309/phase3/Web.sitemap">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>
</xsl:stylesheet>
HTML OUTPUT:
About UsThis page contains basic information about us.
(NOTE: Only the title and description attributes are displayed from the Web.sitemap doc for the About Us page)

I also tried adding another template to the page with a call for the Area Links page, like this:
[/code]...
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    <xsl:template match="sitemap/siteMap/siteMapNode/siteMapNode/siteMapNode[@id='arealinks']">
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet>[/code]

...but I just received this error message:
Exception Details: System.Xml.XPath.XPathException: Expression must evaluate to a node-set.

I also tried this:
Code:
<xsl:apply-templates select="sitemap/siteMap/siteMapNode/siteMapNode/siteMapNode[@id='arealinks']"/>
    </xsl:template>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

...and again, only the two About Us attributes were shown.

If you could let me know what I'm doing wrong, it would be greatly appreciated.

KWilliams
  #6 (permalink)  
Old March 8th, 2007, 06:46 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I've been working with a user from another forum (http://w3schools.invisionzone.com/in...t=0#entry62376), and this is the solution that he's got working:
Web.sitemap
[code]
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="~/default.aspx" title="Home Page" id="default">
    <siteMapNode url="~/aboutus/aboutus.aspx" title="About Us" description="This page contains links to information about us." id="aboutus">
  </siteMapNode>
</siteMap>

aboutus.xsl
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sm="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" exclude-result-prefixes="sm">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:include href="siteMap.xsl" />
    <xsl:template match="/">
        <div id="navigation">
            <xsl:for-each select="document('Web.sitemap')">
                <xsl:apply-templates select="sm:siteMap" />
            </xsl:for-each>
        </div>
        <div id="content">
            <xsl:apply-templates/>
        </div>
    </xsl:template>
</xsl:stylesheet>
sitemap.xsl
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sm="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" exclude-result-prefixes="sm">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template name="map" match="sm:siteMap">
        [list]
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    <xsl:template match="sm:siteMapNode">
        <li>
            <a id="{@id}" href="{@url}">
                <xsl:if test="@description">
                    <xsl:attribute name="title">
                        <xsl:value-of select="@description"/>
                    </xsl:attribute>
                </xsl:if>
                <xsl:value-of select="@title"/>
                <xsl:if test="sm:siteMapNode">
                    <xsl:call-template name="map"/>
                </xsl:if>
            </a>
        </li>
    </xsl:template>
</xsl:stylesheet>
...but it's not working on my side. These two documents are transformed using an ASP.NET Master Page method, like this:

MasterPage.master.vb:
Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    xslTransform_mc.DocumentSource = "docs/xml/aboutus/aboutus.xml" 'xml doc
        xslTransform_mc.TransformSource = "docs/xslt/aboutus/aboutus.xsl" 'xsl doc
End Sub
MasterPage.master:
Code:
<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" Debug="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form id="form1" runat="server">
        <asp:Xml id="xslTransform_mc" runat="server"></asp:Xml>
    </form>
</body>
</html>
If anyone can let me know what I'm doing wrong, it would be very appreciated. Thanks for any and all help.

KWilliams
  #7 (permalink)  
Old March 9th, 2007, 04:49 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What does the aboutus.xml document contain and what exactly is not working? Is the output not as required or the transform not being carried out?

--

Joe (Microsoft MVP - XML)
  #8 (permalink)  
Old March 9th, 2007, 10:26 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The aboutus.xml doc only contains the opening and closing tags, as I'm just working on pulling in the data from the Web.sitemap file, and using it within the XSLT stylesheet. Once I have that working, I'll work on creating filters within the XML document to only display certain nodes from the Web.sitemap page.

The code is not generating errors, but it is creating no output. The output he was able to create would be the following:

Code:
[list]
        <li>
            <a id="default" href="~/default.aspx" title="This is the home page.">Home PageThis is the home page.</a>
            <a id="aboutus" href="~/aboutus/aboutus.aspx" title="This page contains links to information about us.">About UsThis page contains links to information about us.</a>
        </li>
</ul>
I obviously think that the "title" attribute for the links should be title="@title" instead, but I wanted to show you what the actual output should be for the code I included in my previous post. Any suggestions? Thanks for any help.

KWilliams
  #9 (permalink)  
Old March 12th, 2007, 12:00 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, here's the solution to my problem:

After messing around with the URI, I found a way to make it work. I had to change the path for the Web.sitemap doc from http://localhost:1309/phase3/Web.sitemap to c:\Documents and Settings\MYUSERNAME\My Documents\Visual Studio 2005\Projects\phase3\Web.sitemap. I guess since I'm testing this all out on my machine, I'll have to put the direct path to the file on my machine until it's launched. At that point, I should be able to use a regular URI, like http://www.mysite.com/Web.sitemap.

So now that I have the sitemap file being pulled in, I'm next going to work at filtering a specific node from the sitemap using my custom "id" attribute. I'll post any such code when I get it done. Thanks for your help.

KWilliams
  #10 (permalink)  
Old December 7th, 2007, 08:03 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by kwilliams

So now that I have the sitemap file being pulled in, I'm next going to work at filtering a specific node from the sitemap using my custom "id" attribute. I'll post any such code when I get it done. Thanks for your help.

KWilliams
KWilliams, seems like it's been a while since you posted this message, but if you got this done, are you still able to post the code you mentioned above? I'm having a similar problem... Thanks in advance.

Sam



Similar Threads
Thread Thread Starter Forum Replies Last Post
Web.sitemap problem Dmitriy ASP.NET 3.5 Basics 0 October 14th, 2008 03:04 PM
Problem with Web.sitemap? Dmitriy ASP.NET 3.5 Professionals 0 October 14th, 2008 07:14 AM
web.sitemap workidoo BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 June 13th, 2008 08:42 AM
One "web.sitemap" for two languages!!?? MTLedari ASP.NET 2.0 Professional 0 May 18th, 2007 01:42 AM
Web.Sitemap Tremmorkeep BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 July 21st, 2006 05:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.