looping output

J

jimmygoogle

i have an xml file with links to websites but my output only prints the
1st website from each tag - any ideas why?

i have an an xml file that looks sort of like this:

<?xml version="1.0" encoding="utf-8"?>
<site>
<category type='A'>
<link url="abc.com">abc</link>
</category>

<category type='B'>
<link url="abc1.com">abc1</link>
<link url="abc2.com">abc2</link>
<link url="abc3.com">abc3</link>
</category>
</site>

and my xsl looks like this

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:for-each select="site//category">

<span>
<xsl:attribute name="class">footer</xsl:attribute>
<xsl:value-of select="@type" />
</span>

<a>
<xsl:attribute name="href"><xsl:value-of select="link"
/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="link" />
</a>

</xsl:for-each>
<p></p>
</xsl:template>
</xsl:stylesheet>
 
X

xmlator

i have an xml file with links to websites but my output only prints the
1st website from each tag - any ideas why?

Your XSLT says "for each category, do X", where
X is "emit a link". If you want to do more than one
thing for each category, you'll have to explicitly
say that. You could simply introduce another
for-each loop, like so:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:for-each select="site//category">

<span>
<xsl:attribute name="class">footer</xsl:attribute>
<xsl:value-of select="@type" />
</span>

<xsl:for-each select="link">
<a>
<xsl:attribute name="href"><xsl:value-of select="."
/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</a>
</xsl:for-each>

</xsl:for-each>
<p></p>
</xsl:template>
</xsl:stylesheet>

Hope this helps,
Ron Burk
www.xmlator.com
 
J

jimmygoogle

I tried that it doesnt produce anything - I tried another for loop
earlier but it didnt work.
 
X

xmlator

I tried that it doesnt produce anything - I tried another for loop
earlier but it didnt work.

Well, given your data (foo.xml) and my corrected stylesheet
(foo.xslt), I type this:

xsltproc foo.xslt foo.xml >foo.out

and then foo.out contains this:

<?xml version="1.0"?>
<span class="footer">A</span><a href="abc" target="_blank">abc</a><span
class="footer">B</span><a href="abc1" target="_blank">abc1</a><a
href="abc2" target="_blank">abc2</a><a href="abc3"
target="_blank">abc3</a><p/>

Did you cut and paste the stylesheet exactly as posted?
If so, what XSLT processor are you using?

Ron Burk
www.xmlator.com
 
J

jimmygoogle

my bad i typed it wrong - your solution does in fact work - thank your
very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top