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 March 16th, 2006, 11:43 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop and Links

Two questions:

1) How do I grab the info in this XML file (below) and make them all into HTML A HREF links in XSL?

<navLinks>
        <link title="Home">index.html</link>
        <link title="View all our products">products.html</link>
        <link title="About our company">about.html</link>
        <link title="Contact Us">contact.html</link>
</navLinks>

This is what I had (and nothing showed):
        <xsl:for-each select="myPage/navLinks">
            <a href="test.html"><xsl:value-of select="title"/></a>
        </xsl:for-each>

2) I have 6 paragraphs of text in an XML document that goes like this:

<data>
<item>Paragraph 1 ... </item>
<item>Paragraph 2 ... </item>
<item>Paragraph 3 ... </item>
<item>Paragraph 4 ... </item>
<item>Paragraph 5 ... </item>
<item>Paragraph 6 ... </item>
</data>

I tried to use this loop, but it only produced the first paragraph.

        <xsl:for-each select="//data">
            <p><xsl:value-of select="//item"/></p>
        </xsl:for-each>

Thanks for anyones assistance.

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

1. Replace

<xsl:for-each select="myPage/navLinks">
   <a href="test.html"><xsl:value-of select="title"/></a>
</xsl:for-each>

by

<xsl:for-each select="myPage/navLinks/link">
   <a href="{@title}"><xsl:value-of select="."/></a>
</xsl:for-each>

(2) //item selects all the items in the document, and xsl:value-of displays the first thing selected. You want the child item of the current data element, which is just "data".

Both your mistakes suggest that you haven't grasped the concept of relative path expressions which select by navigating from the context node. It's worth doing some reading around this topic.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 17th, 2006, 08:39 AM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mhkay for your help. I have been reading quite a bit on the subject, and even last night I went through two complete tutorials off of Macromedia site which led me in the wrong direction on X-paths. The example loop did not start with the actual child, the child was used within the for-each loop. Anyways, I adjusted mine and it worked fine.

However the advice which was given to grab links from this XML:

<navLinks>
<link title="Home">index.html</link>
<link title="View all our products">products.html</link>
</navLinks>

with this XSL code:

<xsl:for-each select="myPage/navLinks/link">
<a href="{@title}"><xsl:value-of select="."/></a><br/>
</xsl:for-each>

looks like this:

index.html
products.html
about.html
contact.html

and the links goes to the acutal "title". I have tried to reverse the actaul link with the title with no avail. Anyone has a suggestion ...

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

Yes, sorry, a simple error. But I'm surprised you couldn't see how to correct it. Change

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

to

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

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Links that disappear Surre ASP.NET 1.0 and 1.1 Professional 8 February 2nd, 2007 11:15 AM
Generate links darkocobe Classic ASP Basics 1 October 15th, 2004 09:18 AM
Issues with Links bmains Forum and Wrox.com Feedback 3 September 6th, 2004 03:15 PM
how to insert links lanita PHP How-To 4 July 23rd, 2004 03:57 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





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