Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Community Discussions (All other subjects)
 XSL help
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

MarkJH
Senior Member

United Kingdom
1722 Posts

Posted - 09 February 2006 :  15:14:52  Show Profile  Visit MarkJH's Homepage
I have an XML file which follows like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<musicmoz>
<category name="bandname1">
<name>bandname1</name>
   <release type="album" name="xxx" date="1974" />
   <release type="album" name="yyy" date="1977" />
   <release type="album" name="zzz" date="1991" />
   <release type="album" name="aaa" date="1958" />
</category>
<category name="bandname2">
<name>bandname2</name>
   <release type="album" name="xxxx" date="1986" />
   <release type="album" name="yyyy" date="1992" />
   <release type="album" name="zzzz" date="1939" />
   <release type="album" name="aaaa" date="1941" />
   <release type="album" name="bbbb" date="1976" />
   <release type="album" name="cccc" date="1990" />
   <release type="album" name="dddd" date="1990" />
   <release type="album" name="eeee" date="1975" />
</category>
.
.
.
</musicmoz>


stylesheet.xsl follows like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<body style="font-family:Arial,helvetica,sans-serif;font-size:10pt;background-color:#EEEEEE">
				<xsl:text><musicmoz></xsl:text>
				<br/>
				<xsl:for-each select="musicmoz/category">
					<xsl:if test="release/@name">
						<xsl:text><category></xsl:text>
						<br/>
						<xsl:text><artist></xsl:text>
						<xsl:value-of select="name"/>
						<xsl:text></artist></xsl:text>
						<br/>
						<xsl:for-each select="release">
						<xsl:if test="@type='album'">
							<xsl:text><album></xsl:text>
							<xsl:value-of select="@name"/>
							<xsl:text></album></xsl:text>
							<br/>
							<xsl:text><date></xsl:text>
							<xsl:value-of select="@date"/>
							<xsl:text></date></xsl:text>
							<br/>
						</xsl:if>
						</xsl:for-each>
						<xsl:text></category></xsl:text>
						<br/>
					</xsl:if>
				</xsl:for-each>
				<xsl:text></musicmoz></xsl:text>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>


The output I get is like this:

<musicmoz>
<category>
<artist>bandname1</artist>
<album>xxx</album>
<date>1974</date>
<album>yyy</album>
<date>1977</date>
<album>zzz</album>
<date>1991</date>
<album>aaa</album>
<date>1958</date>
</category>
<category>
<artist>bandname2</artist>
<album>xxxx</album>
<date>1986</date>
<album>yyyy</album>
<date>1992</date>
<album>zzzz</album>
<date>1939</date>
<album>aaaa</album>
<date>1941</date>
<album>bbbb</album>
<date>1976</date>
<album>cccc</album>
<date>1990</date>
<album>dddd</album>
<date>1990</date>
<album>eeee</album>
<date>1995</date>
</category>
</musicmoz>


All well and good so far, but how do I get the output to be like this?

<musicmoz>
<category>
<artist>bandname1</artist>
<album1>xxx</album1>
<date1>1974</date1>
<album2>yyy</album2>
<date2>1977</date2>
<album3>zzz</album3>
<date3>1991</date3>
<album4>aaa</album4>
<date4>1958</date4>
</category>
<category>
<artist>bandname2</artist>
<album1>xxxx</album1>
<date1>1986</date1>
<album2>yyyy</album2>
<date2>1992</date2>
<album3>zzzz</album3>
<date3>1939</date3>
<album4>aaaa</album4>
<date4>1941</date4>
<album5>bbbb</album5>
<date5>1976</date5>
<album6>cccc</album6>
<date6>1990</date6>
<album7>dddd</album7>
<date7>1990</date7>
<album8>eeee</album8>
<date8>1995</date8>
</category>
</musicmoz>


Thanks!

Bandlink.net - http://www.bandlink.net/
Bandlink Music Forums - http://www.bandlink.net/forum/

MarkJH
Senior Member

United Kingdom
1722 Posts

Posted - 10 February 2006 :  13:11:32  Show Profile  Visit MarkJH's Homepage
No XML gurus in the house?

Bandlink.net - http://www.bandlink.net/
Bandlink Music Forums - http://www.bandlink.net/forum/
Go to Top of Page

AnonJr
Moderator

United States
5768 Posts

Posted - 10 February 2006 :  16:01:49  Show Profile  Visit AnonJr's Homepage
Let me finish this semester's work first...
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 10 February 2006 :  17:24:17  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
<Edited out dubious article>

Have a look at <xsl:number> - http://www.topxml.com/xsl/tutorials/intro/xsl6a.asp

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.

Edited by - Podge on 10 February 2006 17:36:49
Go to Top of Page

MarkJH
Senior Member

United Kingdom
1722 Posts

Posted - 11 February 2006 :  13:06:27  Show Profile  Visit MarkJH's Homepage
Thanks for the link, Podge.

Using <xsl:number> and position() I might be able to achieve what I'm after.

I'll let you know how I get on.

Bandlink.net - http://www.bandlink.net/
Bandlink Music Forums - http://www.bandlink.net/forum/
Go to Top of Page

MarkJH
Senior Member

United Kingdom
1722 Posts

Posted - 11 February 2006 :  13:25:34  Show Profile  Visit MarkJH's Homepage
Excellent!

This code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<html>
			<body style="font-family:Arial,helvetica,sans-serif;font-size:10pt;background-color:#EEEEEE">
				<xsl:text><musicmoz></xsl:text>
				<br/>
				<xsl:for-each select="musicmoz/category">
					<xsl:if test="release/@name">
						<xsl:text><category></xsl:text>
						<br/>
						<xsl:text><artist></xsl:text>
						<xsl:value-of select="name"/>
						<xsl:text></artist></xsl:text>
						<br/>
						<xsl:for-each select="release">
						<xsl:if test="@type='album'">
							& lt ;album<xsl:number value="position()"/>& gt ;
							<xsl:value-of select="@name"/>
							& lt ;/album<xsl:number value="position()"/>& gt ;
							<br/>
							& lt ;date<xsl:number value="position()"/>& gt ;
							<xsl:value-of select="@date"/>
							& lt ;/date<xsl:number value="position()"/>& gt ;
							<br/>
						</xsl:if>
						</xsl:for-each>
						<xsl:text></category></xsl:text>
						<br/>
					</xsl:if>
				</xsl:for-each>
				<xsl:text></musicmoz></xsl:text>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>


Produced almost exactly what I wanted:

<artist>bandname1</artist>
<album1> xxx </album1>
<date1> 1974 </date1>
<album2> yyy </album2>
<date2> 1977 </date2>
<album3> zzz </album3>
<date3> 1991 </date3>
<album4> aaa </album4>
<date4> 1958 </date4>
</category>


except now I have spaces either side of the value of each element. I'm sure there's a way around that though, right?

Bandlink.net - http://www.bandlink.net/
Bandlink Music Forums - http://www.bandlink.net/forum/

Edited by - MarkJH on 11 February 2006 13:27:53
Go to Top of Page

MarkJH
Senior Member

United Kingdom
1722 Posts

Posted - 12 February 2006 :  08:43:42  Show Profile  Visit MarkJH's Homepage
Fixed the blank space issue by putting the three lines of code in red for <album></album> and the three lines of code for <date></date> on the same line.

Podge, thank you!

Bandlink.net - http://www.bandlink.net/
Bandlink Music Forums - http://www.bandlink.net/forum/
Go to Top of Page

Podge
Support Moderator

Ireland
3776 Posts

Posted - 13 February 2006 :  08:34:29  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Very good. XSL isn't easy but you seem to have a handle on it.
Are you using a tool to help you generate the XSL file?

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

MarkJH
Senior Member

United Kingdom
1722 Posts

Posted - 13 February 2006 :  11:22:09  Show Profile  Visit MarkJH's Homepage
I'm using XMLSpy to help me with the correct syntax which is a useful tool. Though, there's still quite a lot of trial and error in getting it right. Then, I'm loading up the original XML file in Firefox then cutting and pasting the 'XML' output. It's a one hit thing so that's good enough for me.

I'd use Internet Explorer but since upgrading to v7, I've had problems loading offline XML files onto it.

Bandlink.net - http://www.bandlink.net/
Bandlink Music Forums - http://www.bandlink.net/forum/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.34 seconds. Powered By: Snitz Forums 2000 Version 3.4.07