xsl-list
[Top] [All Lists]

Bread-crumbs nav from nested hierarchy in reverse order

2003-08-18 20:51:50
I'm trying to create bread-crumbing for the navigation of a website. I
have the site hierarchy in a forwards nested document like below. The
only way I could figure out how to get the bread crumbs at all was to
get a backwards result. Once I have the result doc, I dump it as a
srting to the browser. Got any ideas of a simple way to reverse the
order in xsl, or a better way to write my first xsl?

Here's how my xml document is formed.

<page>
        <title>Home Page<title>
        <pageid>1</pageid>
        <page>
                <title>Parent Page</title>
                <pageid>2</pageid>
                <page>
                        <title>This Page</title>
                        <pageid>3</pageid>
                </page>
        <page>
</page>

Here's the xslt I wrote up.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:param name="pageid" select="23"/>
        <xsl:template match="/">
                <docroot>
                        <xsl:apply-templates mode="first"/>
                </docroot>
        </xsl:template>
        <xsl:template match="node()" mode="first">
                <xsl:apply-templates select="page" mode="first"/>
                <xsl:if test="pageid=$pageid">
                        <xsl:call-template name="bread-crumb"/>
                </xsl:if>
        </xsl:template>
        <xsl:template match="node()" mode="ancestor">
                <xsl:call-template name="bread-crumb"/>
        </xsl:template>
        <xsl:template name="bread-crumb">
                <xsl:text>&gt;&gt;</xsl:text>
                <xsl:element name="a">
                        <xsl:attribute
name="href">index.cfm?pageid=<xsl:value-of
select="pageid"/></xsl:attribute>
                        <xsl:value-of
select="normalize-space(pagetitle)"/>
                </xsl:element>
                <xsl:if test="../pageid">
                        <xsl:apply-templates select=".."
mode="ancestor"/>
                </xsl:if>
        </xsl:template>
</xsl:stylesheet>

This is what I end up with.

<?xml version="1.0" encoding="UTF-8"?>
<docroot>
        <a href="index.cfm?pageid=23">Laptop Requirements</a>
        <a href="index.cfm?pageid=21">Computing</a>
        <a href="index.cfm?pageid=20">Once you're admitted</a>
        <a href="index.cfm?pageid=1">Admissions</a>
</docroot>

Here's the browser view.

Laptop Requirements>>Computing>>Once you're admitted>>Admissions



Josh Simerman
Graduate Assistant, Web Developer
Systems & Accounting Graduate Programs
Indiana University, Kelley School of Business


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>