Forum Moderators: open

Message Too Old, No Replies

Loops and Links

Just geeting my feet wet ...

         

MacJr

3:59 am on Mar 17, 2006 (gmt 0)

10+ Year Member



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.

mikesmith76

10:04 am on Mar 17, 2006 (gmt 0)

10+ Year Member



For 2) try this

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

Think that should work

MacJr

12:06 pm on Mar 17, 2006 (gmt 0)

10+ Year Member



Hey there,

Thanks for the suggestion:

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

but, it still only grabs the first paragraph only. I actually tried using only a . as in

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

Thhis grabs all of them, but without any breaks. Maybe I could this with CSS?

MacJr

12:31 pm on Mar 17, 2006 (gmt 0)

10+ Year Member



The loop works. I had the wrong X-path. It should have been:

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

Thanks to mikesmith.

One issue however goes to the creation of links with the following XML example data:

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

I have used:

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

but it produces the links underlined and the link goes to the name or title:

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

How can I grab the title and reverse the two?

mikesmith76

12:59 pm on Mar 17, 2006 (gmt 0)

10+ Year Member



sorry just reread the original post. regarding the 2nd xml format shouldn't you have 2 loops? the loop you currently have in place will loop through all data elements, am i correct in assuming you have multiple data blocks in the xml file. e.g.

<data>
<item>Paragraph 1 ... </item>
<item>Paragraph 2 ... </item>
<item>Paragraph 3 ... </item>
</data>
<data>
<item>Paragraph 1 ... </item>
<item>Paragraph 2 ... </item>
</data>

is this valid in your document?

MacJr

1:10 pm on Mar 17, 2006 (gmt 0)

10+ Year Member



Hi Mike,

No, there is only one <data> block. The following loop worked with your help. I did not know to place the clhild in the actual for-each expression.

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

I now have some nicely formed paragraphs.

mikesmith76

1:25 pm on Mar 17, 2006 (gmt 0)

10+ Year Member



ok glad you got your problem sorted