7. Track number of ‘All Blues’ song on ‘Kind of Blue’ CD

8. Name of second track on ‘Cookin’ CD

9. Total cost to purchase all 3 CDs (UK prices)

Please see attached file for XML....

I need to have code for XSL..... I couldn't figure out for number 7 , 8, 9. let me know.

Recommended Answers

All 3 Replies

Hi,

can you please give the xslt code which you have tried????


Cheers,
Mahesh :)

Hi,

May be following code can help you....

here i assume that, there are only 3 <CD> under the <CDs> tag...

<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:output method="html"/>

<xsl:template match="Specials">
	<html>
		<head>
			<title><xsl:apply-templates select="./Title"/></title>
		</head>
		<body>
			<xsl:apply-templates />
		</body>
	</html>
</xsl:template>

<xsl:template match="Specials/Title" />

<xsl:template match="//Message" />

<xsl:template match="//CDs">

<xsl:variable name="t1" select="./CD[1]//PriceUK" />
<xsl:variable name="t2" select="./CD[2]//PriceUK" />
<xsl:variable name="t3" select="./CD[3]//PriceUK" />

<xsl:for-each select="./CD">
		<div>
			<p>Title: <xsl:value-of select="@title" /></p>
			<p>Artist: <xsl:value-of select="./Artist" /></p>
			<p>Total Tracks: <xsl:value-of select="count(./Tracks/Track)" /></p>

			<xsl:for-each select="./Tracks/Track">
				<ul>
					<li>Track: <xsl:value-of select="." /></li>
					<li>Track Length: <xsl:value-of select="@length" /></li>
				</ul>
			</xsl:for-each>


		</div><hr />
	</xsl:for-each>

<p>If customer select only first CD then, Price in UK: <xsl:value-of select="$t1" /></p>
<p>If customer select only Second CD then, Price in UK: <xsl:value-of select="$t2" /></p>
<p>If customer select only Third CD then, Price in UK: <xsl:value-of select="$t3" /></p>
<hr />
<p>If customer select First and Second CD then, Price in UK: <xsl:value-of select="$t1 + $t2" /></p>
<p>If customer select First and Third CD then, Price in UK: <xsl:value-of select="$t1 + $t3" /></p>
<p>If customer select Second and Third CD then, Price in UK: <xsl:value-of select="$t2 + $t3" /></p>
<p>If customer select All CDs then, Price in UK: <xsl:value-of select="$t1 + $t2 + $t3" /></p>
</xsl:template>



</xsl:stylesheet>

OUTPUT will looks like

<html>
<head>
<title></title>
</head>
<body>
<div>
<p>Title: Kind of Blue</p>
<p>Artist: Miles Davis</p>
<p>Total Tracks: 5</p>
<ul>
<li>Track: So What</li>
<li>Track Length: 9:22</li>
</ul>
<ul>
<li>Track: Frddie Freeloader</li>
<li>Track Length: 9:46</li>
</ul>
<ul>
<li>Track: Blue in Green</li>
<li>Track Length: 5:37</li>
</ul>
<ul>
<li>Track: All Blues</li>
<li>Track Length: 11:33</li>
</ul>
<ul>
<li>Track: Flamenco Sketches</li>
<li>Track Length: 9:26</li>
</ul>
</div><hr>
<div>
<p>Title: Cookin</p>
<p>Artist: Miles Davis</p>
<p>Total Tracks: 4</p>
<ul>
<li>Track: My Funny Valentine</li>
<li>Track Length: 5:57</li>
</ul>
<ul>
<li>Track: Blues by Five</li>
<li>Track Length: 9:53</li>
</ul>
<ul>
<li>Track: Airegin</li>
<li>Track Length: 4:22</li>
</ul>
<ul>
<li>Track: Tune-Up</li>
<li>Track Length: 13:03</li>
</ul>
</div><hr>
<div>
<p>Title: Blue Train</p>
<p>Artist: John Coltrane</p>
<p>Total Tracks: 5</p>
<ul>
<li>Track: Blue Train</li>
<li>Track Length: 10:39</li>
</ul>
<ul>
<li>Track: Moment's Notice</li>
<li>Track Length: 9:06</li>
</ul>
<ul>
<li>Track: Locomotion</li>
<li>Track Length: 7:11</li>
</ul>
<ul>
<li>Track: I'm Old Fashioned</li>
<li>Track Length: 7:55</li>
</ul>
<ul>
<li>Track: Lazy Bird</li>
<li>Track Length: 7:03</li>
</ul>
</div><hr>
<p>If customer select only first CD then, Price in UK: 8.39</p>
<p>If customer select only Second CD then, Price in UK: 5.59</p>
<p>If customer select only Third CD then, Price in UK: 6.29</p>
<hr>
<p>If customer select First and Second CD then, Price in UK: 13.98</p>
<p>If customer select First and Third CD then, Price in UK: 14.68</p>
<p>If customer select Second and Third CD then, Price in UK: 11.879999999999999</p>
<p>If customer select All CDs then, Price in UK: 20.27</p>
</body>
</html>

Cheers,
Mahesh :)

from sample
looking for Total duration

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" indent="yes"/>
	<xsl:key name="sum" match="//@length" use="number(translate(.,':','.'))"/>
	<xsl:template match="/">
		<xsl:apply-templates select="Specials"/>
	</xsl:template>

	<xsl:template match="Specials">
		<html>
			<head>
				<title>
					<xsl:apply-templates select="Title"/>
				</title>
			</head>
			<body>
				<xsl:apply-templates select="CDs"/>
				<xsl:call-template name="question789"/>
			</body>
		</html>
	</xsl:template>


	<xsl:template match="CDs">

		<xsl:apply-templates select="CD"/>
	</xsl:template>
	<xsl:template match="CD">
		<div>
			<p>Title: <xsl:value-of select="@title"/></p>
			<p>Artist: <xsl:value-of select="./Artist"/></p>
			<p>Total Tracks: <xsl:value-of select="count(./Tracks/Track)"/></p>
			<xsl:apply-templates select="Tracks"/>
		</div>
	</xsl:template>
	<xsl:template match="Tracks">
		<xsl:apply-templates select="Track"/>
		<p>
			<xsl:call-template name="timesum">
				<xsl:with-param name="km" select="child::Track/@length"/>
			</xsl:call-template>
		</p>
	</xsl:template>
	<xsl:template match="Track">
		<ul>
			<li>Track: <xsl:value-of select="."/></li>
			<li>Track Length: <xsl:value-of select="@length"/></li>
		</ul>
	</xsl:template>
	<xsl:template name="timesum">
		<xsl:param name="km"/>
		<xsl:param name="ak" select="1"/>
		<xsl:param name="min" select="0"/>
		<xsl:param name="hour" select="0"/>
		<xsl:variable name="c" select="count($km)"/>

		<xsl:choose>
			<xsl:when test="$ak &lt;= $c">

				<xsl:variable name="tmin" select="substring-after($km[$ak],':')"/>
				<xsl:variable name="thour" select="substring-before($km[$ak],':')"/>

				<xsl:call-template name="timesum">
					<xsl:with-param name="ak" select="$ak+1"/>
					<xsl:with-param name="km" select="$km"/>
					<xsl:with-param name="hour" select="$hour + $thour"/>
					<xsl:with-param name="min" select="$min + $tmin"/>
				</xsl:call-template>
			</xsl:when>

			<xsl:otherwise>
				<xsl:value-of select="concat('Total duration',$hour+($min -$min mod 60) div 60,':',format-number($min mod 60,'00'))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="question789">
		<h1>answer of question 7,8,9</h1>
		<p>Track number of ‘All Blues’ song on ‘Kind of Blue’ CD</p>
		<p>count(/Specials/CDs/CD[@title='Kind of Blue']/Tracks/Track[.='All Blues']/preceding-sibling::Track)+1</p>
		<h3>
			<xsl:value-of select="count(/Specials/CDs/CD[@title='Kind of Blue']/Tracks/Track[.='All Blues']/preceding-sibling::Track)+1"/>
		</h3>
		<p>Name of second track on ‘Cookin’ CD</p>
		<p>/Specials/CDs/CD[@title='Cookin']/Tracks/Track[2]</p>
		<h3>
			<xsl:value-of select="/Specials/CDs/CD[@title='Cookin']/Tracks/Track[2]"/>
		</h3>
		<p>Total cost to purchase all 3 CDs (UK prices)</p>
		<p>sum(/Specials/CDs//CD/PriceUK)</p>
		<h3>
			<xsl:value-of select="sum(/Specials/CDs//CD/PriceUK)"/>
		</h3>
	</xsl:template>
</xsl:stylesheet>

<xsl:value-of select="count(/Specials/CDs/CD[@title='Kind of Blue']/Tracks/Track[.='All Blues']/preceding-sibling::Track)+1"/>

<xsl:value-of select="/Specials/CDs/CD[@title='Cookin']/Tracks/Track[2]"/>

<xsl:value-of select="sum(/Specials/CDs//CD/PriceUK)"/>

Hemlut Hagemann

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.