Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old October 5th, 2006, 06:22 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Crumb trail

I am fairly new to xsl, I am trying to produce a navigation menu and am having difficulty creating a crumb trail. I have managed to produce the menu. The xml structure I am working with is pasted below. I'd appreciate if I could have some help with this. I don't have have much idea how I can iterate from a sibling all the way to root node and output the node value in order i.e. the root node first, then the first child, then the third etc..

Thanks.

<?xml version="1.0" encoding="UTF-8"?>


<menuitem name="School Of Dadada" id="1" url="/navigation/school.php">
    <menuitem name="Undergraduate" id="1" url="/navigation/undergraduate.php">
        <menuitem name="About Us" id="1" url="/navigation/undergraduate/aboutus.php">
            <menuitem name="University Structure" id="1_1" url="/navigation/undergraduate/unversitystructure.php"/>
            <menuitem name="Facts and Figures" id="1_2" url="/navigation/undergraduate/factsandfigures.php"/>
        </menuitem>
        <menuitem name="Study" id="2" url="/navigation/undergraduate/study.php">
            <menuitem name="Our Reputation" id="2_1" url="/navigation/undergraduate/ourreputation.php"/>
            <menuitem name="Courses" id="2_2" url="/navigation/undergraduate/courses.php"/>
        </menuitem>
    </menuitem>
    <menuitem name="Postgraduate" id="1" url="/navigation/postgraduate.php">
        <menuitem name="About Us" id="1" url="/navigation/postgraduate/aboutus.php">
            <menuitem name="University Structure" id="1_1" url="/navigation/postgraduate/unversitystructure.php"/>
            <menuitem name="Facts and Figures" id="1_2" url="/navigation/postgraduate/factsandfigures.php"/>
        </menuitem>
        <menuitem name="Study" id="2" url="/navigation/postgraduate/study.php">
            <menuitem name="Our Reputation" id="2_1" url="/navigation/postgraduate/ourreputation.php"/>
            <menuitem name="Courses" id="2_2" url="/navigation/postgraduate/courses.php"/>
        </menuitem>
    </menuitem>
</menuitem>

 
Old October 5th, 2006, 06:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In essence, it's just

<xsl:for-each select="ancestor-or-self::menuitem">
  <xsl:text> > </xsl:text>
  <xsl:value-of select="name"/>
</xsl:for-each>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 5th, 2006, 07:06 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Michael,

Thank you for the quick reply, I've tried the code and no output is produced, this is what I have done...

<xsl:template name="crumb_trail">
    <xsl:param name="header_element"/>

<xsl:for-each select="ancestor-or-self::header_element">
          <li class="nav-secondary-breadcrumb">
            <xsl:text disable-output-escaping='yes'>&lt;a href="</xsl:text>
            <xsl:value-of disable-output-escaping='yes' select="@url"/>
            <xsl:text disable-output-escaping='yes'>"&gt;</xsl:text>
            <xsl:value-of select="@name" />
            <xsl:text disable-output-escaping='yes'>&lt;/a&gt;</xsl:text>
        </li>
</xsl:for-each>


</xsl:template>

where header_element the node representing the current page in the navigation. Have I got the syntax wrong.

Regards Mukhtar.

 
Old October 5th, 2006, 08:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, NEVER use disable-output-escaping for this kind of job. It's an advanced facility to be used on very rare occasions, for example if you're dealing with input that has been double-escaped, but by using it here you're writing code that will only work in a restricted set of environments, for example it won't work on Firefox. It also means that the HTML serializer can't do it's stuff properlt for example by escaping URLs as required. In any case, it's ludicrously verbose. What you want is:

<a href="{@url}">
  <xsl:value-of select="@name" />
</a>

Now to the point in hand. The chances are that your code doesn't work primarily because the initial context is wrong. You didn't show enough code to reveal what the initial context is, and this probably indicates that you haven't understood the importance of getting it right. A path expression will never select the right nodes if you start from the wrong place.

But also, the code I showed you used ancestor-or-self::menuitem. That's because the elements it's selecting are called menuitem. I've no idea why you changed it to ancestor-or-self::header_element. It sugegsts that you're guessing, and working by trial and error. which is not a good way of learning a new language. Find a book or online guide that suits your technical level, read it, and work through the exercises until you understand what's going on.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 5th, 2006, 08:24 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Michael,

Thank you for correcting the mistake, you are right I am coding on a trial and error basis, the pure reason for this is I have about 3 days to get this software up and running, hence it would be very difficult to learn xsl and have the software working by the deadline.

I apologise for not pasting the full code, as I thought it will make it complicated to focus on the part I am struggling with.

Regards Mukhtar



 
Old October 5th, 2006, 09:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>the pure reason for this is I have about 3 days to get this software up and running

Heard it before. If you don't have time to learn to use a new technology properly, then don't use it at all. You won't be doing anyone a favour.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 5th, 2006, 09:26 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I get it working I get paid for it, so yes I am doing my self a favour.

 
Old October 5th, 2006, 10:48 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you can only get it working by Michael doing the work what's his cut?

--

Joe (Microsoft MVP - XML)
 
Old October 6th, 2006, 03:15 AM
Registered User
 
Join Date: Oct 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well its a bit late now, I've got it working. Thanks for the replies chaps.

Mukhtar.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an Audit Trail Jade08 Pro VB 6 3 October 23rd, 2008 12:44 AM
Infopath Audit Trail sqaengineer Infopath 0 August 13th, 2007 10:10 AM
BizTalk trail download josekarun Biztalk 0 August 4th, 2006 10:52 AM
JavaScript trail for books varelg Javascript 1 April 25th, 2004 07:39 PM
XML/XSL BreadCrumb Trail fortgjort XSLT 8 August 28th, 2003 04:26 AM





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