Click here to Skip to main content
16,004,458 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have the following XMl File, I have to generate xsl with the list of all Titles and all their artist.

Can some one please help me

XML
<?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>Stop</title>
        <artist>Sam Brown</artist>
        <country>UK</country>
        <company>A and M</company>
        <price>8.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Stop</title>
        <artist>Sam12 Brown</artist>
        <country>UK</country>
        <company>A and M</company>
        <price>8.90</price>
        <year>1988</year>
    </cd>

</catalog>
Posted
Comments
AspDotNetDev 14-Sep-10 17:37pm    
XSL is used to transform one document into a document of another form (e.g., XML to HTML). What are you using the XSL for?
ArchanCM 14-Sep-10 17:41pm    
Yes , I am using for the same . To display/transfer data from XMl to HTMl using XSL.

This page shows how to group items:

Store the value in a string variable, then trim the last 3 characters off using string-length and substring:

As an alternative to string trimming, you can use an if statement, the position function, and the count function to determine whether or not to append the last comma (or the first comma, which would prevent you from having to use the count function):

Next time, be a little more specific when asking your question... there is no way I could have known that's what you wanted by reading your original question.
 
Share this answer
 
Comments
ArchanCM 16-Sep-10 17:51pm    
Thank you so much for posting the links ...
ArchanCM 16-Sep-10 17:56pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
AspDotNetDev 16-Sep-10 19:00pm    
I have only 1 vote and it is a value of 2. Did you accidentally change your 5-vote to a 2-vote?
I have tried this



XML
<?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> : </xsl>
               <xsl:value-of select="artist"/>
               <xsl> , </xsl>
 </xsl:for-each>
</body>
  </html>
</xsl:template>
</xsl:stylesheet>


Output will be like : Empire Burlesque : Bob Dylan , Hide your heart : Bonnie Tyler , Hide your heart : Bonnie111 Tyler ,

I want output like
Empire Burlesque : Bob Dylan , Hide your heart : Bonnie Tyler , Bonnie111 Tyler
Can you please give some suggestion on this...
 
Share this answer
 
Comments
AspDotNetDev 15-Sep-10 13:37pm    
So you want to combine artists when they have the same song, and you want to remove the trailing comma?
ArchanCM 15-Sep-10 13:39pm    
Yes.
Please suggest ...
Solution :
XML
<?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>
 
Share this answer
 
v2
Comments
AspDotNetDev 16-Sep-10 19:22pm    
Neat, I didn't know there was a "last()" function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900