p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Calling Web.Sitemap from XSLT (http://p2p.wrox.com/showthread.php?t=54760)

kwilliams March 1st, 2007 11:27 AM

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

joefawcett March 1st, 2007 11:32 AM

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

--

Joe (Microsoft MVP - XML)

kwilliams March 1st, 2007 11:38 AM

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

joefawcett March 1st, 2007 12:50 PM

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

--

Joe (Microsoft MVP - XML)

kwilliams March 1st, 2007 01:22 PM

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

kwilliams March 8th, 2007 06:46 PM

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

joefawcett March 9th, 2007 04:49 AM

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)

kwilliams March 9th, 2007 10:26 AM

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

kwilliams March 12th, 2007 12:00 PM

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

dude153 December 7th, 2007 08:03 PM

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



All times are GMT -4. The time now is 06:46 AM.

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