Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2010, 06:29 PM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generating XSL from XML

Hi All,

I have the following XMl file

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>


I have to generate the XSL file to diplay data in HTML , where data out oput should display the title name followed by list of all the artist names with comma seperated .

I have written the following code..

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl><b> : </b> </xsl>
<xsl:value-of select="artist"/>
<xsl:if test="position() != last()"> <b> , </b></xsl:if>


</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>


which will give result as

Empire Burlesque : Bob Dylan , Hide your heart : Bonnie Tyler , The very best of : Cat Stevens , The very best of : Joe Cocker


But I want result as

Empire Burlesque : Bob Dylan , Hide your heart : Bonnie Tyler , The very best of : Cat Stevens , Joe Cocker

someone please help me , by providing the suggestion to resolve this.

Thanks in advance
 
Old September 16th, 2010, 02:23 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try with the below:
Code:
<xsl:key name="title" match="//cd" use="title" />
<xsl:template match="catalog">
<xsl:for-each select="cd[count(.|key('title', title)[1]) = 1]">
<xsl:value-of select="title"/><xsl><b> : </b> </xsl>
<xsl:apply-templates select="key('title', title)"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="cd">
<xsl:value-of select="artist"/>
<xsl:if test="position() != last()"> <b> , </b></xsl:if>
</xsl:template>
__________________
Rummy
 
Old September 16th, 2010, 05:53 PM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Solution is as follows

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="contacts-by-surname" match="cd" use="title" />
<xsl:template match="catalog">
<xsl:for-each select="cd[count(. | key('contacts-by-surname', title)[1]) = 1]">
<xsl:value-of select="title" />
<b> : </b>
<xsl:for-each select="key('contacts-by-surname', title)">
<xsl:value-of select="artist" />
<xsl:if test="position() != last()"> <b> , </b></xsl:if>
</xsl:for-each>
<xsl:if test="position() != last()"> <br /></xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating XML m_rajib Crystal Reports 0 May 4th, 2006 07:51 AM
generating XML through PHP Ashleek007 Beginning PHP 1 January 27th, 2006 08:16 AM
Generating documentation from the XML brko C# 2005 0 January 23rd, 2006 06:24 AM
Help generating text from xml billclay XSLT 1 April 21st, 2005 04:18 AM
Generating XML tags on the fly francislang XSLT 3 September 6th, 2004 05:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.