Hi,

I need the name of the XML tabs. I'll use an example to explain what i want.

XML-
<?xml version="1.0" encoding="ISO-8859-1"?>

<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>Greatest Hits</title>
		<artist>Dolly Parton</artist>
		<country>USA</country>
		<company>RCA</company>
		<price>9.90</price>
		<year>1982</year>
	</cd>
</catalog>

now i want the values as 'title', 'artist', 'country' and so on.
i tried to do it through XSL however after all attempts i ended up with the values of title (i.e. Empire Burlesque, Hide your heart, Greatest Hits), artist (i.e. Bob Dylan, Bonnie Tyler, Dolly Parton) and similarly for all other tabs.

I am not sure whether this is possible or not.

The output i want is like-

<table>
<tr>
<th>
title
</th>
<th>
artist
</th>
<th>
country
</th>
<th>
company
</th>
<th>
price
</th>
<th>
year
</th>
</tr>
</table>

Please suggest.

Well this is pretty much the most basic XSLT you could write. I'll assume this is a homework assignment. This entire XSLT could be learned from W#C Schools http://www.w3schools.com/xsl/default.asp. But you need to actually try.

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

	<xsl:template match="/">
		<html>
			<body>
				<xsl:apply-templates select="catalog"/>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="catalog">
		<table>
			<tr>
				<th>title</th>
				<th>artist</th>
				<th>country</th>
				<th>company</th>
				<th>price</th>
				<th>year</th>
			</tr>
			<xsl:apply-templates select="cd"/>
		</table>
	</xsl:template>

	<xsl:template match="cd">
		<tr>
			<td>
				<xsl:apply-templates select="title"/>
			</td>
			<td>
				<xsl:apply-templates select="artist"/>
			</td>
			<td>
				<xsl:apply-templates select="country"/>
			</td>
			<td>
				<xsl:apply-templates select="price"/>
			</td>
			<td>
				<xsl:apply-templates select="year"/>
			</td>
		</tr>
	</xsl:template>

	<xsl:template match="title | artist | coutnry | price | year">
		<xsl:value-of select="."/>
	</xsl:template>
</xsl:stylesheet>

xml to html with css

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" version="1.0" indent="yes"/>
	<xsl:template match="/">
		<html>
			<style type="text/css">table
{
width:100%;
border-collapse:collapse;
}
tr
{
background-color:#9acd32;
}
tr.even
{
background-color:#9a0000;
}
tr.odd
{
background-color:#009a00;
}
table,th,td
{
border: 2px solid black;
margin:2px;
}
td
{
padding:5px;
text-align:center;
}</style>
			<body>
				<h2>My CD Collection</h2>
				<xsl:apply-templates select="catalog"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="catalog">
		<table>
			<tr>
				<th>Pos</th>
				<th>Title</th>
				<th>Artist</th>
				<th>country</th>
				<th>company</th>
				<th>price</th>
				<th>year</th>
			</tr>
			<xsl:apply-templates select="cd"/>
		</table>
	</xsl:template>

	<xsl:template match="cd">
		<tr>
			<xsl:choose>
				<xsl:when test="position() mod 2 = 1">
					<xsl:attribute name="class">odd</xsl:attribute>
				</xsl:when>
				<xsl:when test="position() mod 2 = 0">
					<xsl:attribute name="class">even</xsl:attribute>
				</xsl:when>
			</xsl:choose>
			<td>
				<xsl:value-of select="position()"/>
			</td>
			<!--here choose which display and oder -->
			<xsl:apply-templates select="title|artist|country|company|price|year"/>
		</tr>
	</xsl:template>
	<xsl:template match="*">
		<td>
			<xsl:value-of select="."/>
		</td>
	</xsl:template>
</xsl:stylesheet>

Hi xml_looser and iceandrews,

I believe you both got me wrong , its not that i want to avoid working or trying to get help without actually trying on my own.

whatever you both have suggested help me in getting the values of the 'title', 'artist' and all other nodes. however what i want is to get the name of the tab as in this example they are 'title', 'artist' and all others.

i don't want to hardcode them in <th> part as you've done-

<th>title</th>
<th>artist</th>
<th>country</th>
<th>company</th>
<th>price</th>
<th>year</th>

I want these values by the help of XSL.
I hope i made myself clear. Please assist.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" version="1.0" indent="yes"/>
	<xsl:template match="/">
		<html>
			<style type="text/css">table
{
width:100%;
border-collapse:collapse;
}
tr
{
background-color:#9acd32;
}
tr.even
{
background-color:#9a0000;
}
tr.odd
{
background-color:#009a00;
}
table,th,td
{
border: 2px solid black;
margin:2px;
}
td
{
padding:5px;
text-align:center;
}</style>
			<body>
				<h2>My CD Collection</h2>
				<xsl:apply-templates select="catalog"/>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="catalog">
		<table>
			<tr>
				<th>Pos</th>
				<xsl:apply-templates select="cd[1]/*"  mode="head"/>
			</tr>
			<xsl:apply-templates select="cd"/>
		</table>
	</xsl:template>

	<xsl:template match="cd">
		<tr>
			<xsl:choose>
				<xsl:when test="position() mod 2 = 1">
					<xsl:attribute name="class">odd</xsl:attribute>
				</xsl:when>
				<xsl:when test="position() mod 2 = 0">
					<xsl:attribute name="class">even</xsl:attribute>
				</xsl:when>
			</xsl:choose>
			<td>
				<xsl:value-of select="position()"/>
			</td>
			<!--here choose which display and oder -->
			<xsl:apply-templates select="*"/>
		</tr>
	</xsl:template>
	<xsl:template match="*">
		<td>
			<xsl:value-of select="."/>
		</td>
	</xsl:template>
	<xsl:template match="*" mode="head">
		<th>
			<xsl:value-of select="local-name()"/>
		</th>
	</xsl:template>
</xsl:stylesheet>

Thanks a lot xml_looser,

"<xsl:value-of select="local-name()"/>", this is what i was looking for. i was not aware of 'local-name()'.

Thanks once again.

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.