[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] for-each question


Subject: RE: [xsl] for-each question
From: "Vasu Chakkera" <vasucv@xxxxxxxxxxx>
Date: Tue, 13 Aug 2002 14:33:32 +0000

Here it goes Holmberg ..

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="html"/>
<xsl:template match="test">
The Id is<br/>
<xsl:value-of select="id"/><br/><br/>
The Books are:<hr/>
<xsl:for-each select="book">
<xsl:value-of select="."/><br/>
</xsl:for-each>
<br/>
<xsl:for-each select="magazine">
<xsl:value-of select="."/><br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

It would be better if your xml structure looks like.

<test id = "TestId">
<book>ABC123</book>
<book>ABC456</book>
<book>ABC789</book>
<magcategory>General</magcategory>
<magazine>MAG1</magazine>
<magazine>MAG2</magazine>
<magazine>MAG3</magazine>
</test>

and if Mag1, Mag2 and Mag3 are of type General, and if you expect to have more magazines of other types .. then a better structure would be..
<magazines category = "General">
<magazine>MAG1</magazine>
<magazine>MAG2</magazine>
<magazine>MAG3</magazine>
</magazines>
so that you may later on add other magazines of other types like Science
for eg.,


<magazines category = "Science">
<magazine>MAG4</magazine>
<magazine>MAG5</magazine>
<magazine>MAG6</magazine>
</magazines>


Thanks Vasu.

From: Holmberg Rick-ra0119 <Rick.Holmberg@xxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: "'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'" <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: [xsl] for-each question
Date: Tue, 13 Aug 2002 06:47:38 -0700

Ok that got me closer but I need to take it one step further. I need my html to show the id as well. I don't want it to show me the magcategory though...ie
If my xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<test>
<id>TestId</id>
<book>ABC123</book>
<book>ABC456</book>
<book>ABC789</book>
<magcategory>General</magcategory>
<magazine>MAG1</magazine>
<magazine>MAG2</magazine>
<magazine>MAG3</magazine>
</test>


I would like the html to read:

The ID is
TestId

The Books are:
ABC123
ABC456
ABC789

MAG1
MAG2
MAG3

I tried to just add another template but that does not work. Any help would be appreciated. Thanks!



-----Original Message-----
From: Vasu Chakkera [mailto:vasucv@xxxxxxxxxxx]
Sent: Tuesday, August 13, 2002 2:58 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] for-each question


The problem is your template matches each book element. this is equivalent to a pseudo code for each book { The books are: Book-Name } which will produce "The Books are" Thrice( in your case). You have to take the "The books are" out of the loop.

the following for-loop would help you
( this xsl produces a HTML result )

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="html"/>
<xsl:template match="test">
The Books are:<hr/>
<xsl:for-each select="book">
<xsl:value-of select="."/><br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

This would give a result
The Books are:
______________
ABC123
ABC456
ABC789

Hope this helps
Vasu
>From: Holmberg Rick-ra0119 <Rick.Holmberg@xxxxxxxxxxxx>
>Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>Subject: [xsl] for-each question
>Date: Mon, 12 Aug 2002 16:56:44 -0700
>
>I am new to Xsl and am trying to parse a xml document into a html doc.  I
>would like to have a heading above several elements but don't want that
>heading to show up above each element.  I am sure this is simple but I
>can't seem to quite get it.  Here is what I have.
>
>If my xml file looks like this
><?xml version="1.0" encoding="UTF-8"?>
><test>
><id>TestId</id>
><book>ABC123</book>
><book>ABC456</book>
><book>ABC789</book>
></test>
>
>I would like the html to read
>The Books are:
>ABC123
>ABC456
>ABC789
>
>My Stylesheet segment is:
>
><xsl:stylesheet version="1.0"
>xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
><xsl:output omit-xml-declaration="yes"/>
><xsl:output method="html"/>
>
><xsl:template match="/">
>     <xsl:copy>
>       <xsl:apply-templates/>
>     </xsl:copy>
></xsl:template>
>
><xsl:template match="book">
><I>
>The Books are:
><xsl:apply-templates select="book" />
><xsl:apply-templates/>
></I>
></xsl:template>
>
>
>With this XSL file I am getting the 'The Books are:' printed 3 times.  I
>only need it once.  I figure I have to use a for-each but I can't seem to
>get that to work correctly.  Can anyone help here?
>
>Thanks,
>Rick
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




_________________________________________________________________ Chat with friends online, try MSN Messenger: http://messenger.msn.com


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


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




_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


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




Current Thread
Keywords