XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 257
Number of posts: 456

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Puzzled about how this works...!!! Posted by deostroll on 16 Nov 2006 at 5:44 AM
This is a problem involving xml schema: I am supposed to print the details in tabular format!

xsl file code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="product">
		<xsl:value-of select="."/>
		<xsl:if test="position()!=last()">, </xsl:if>
	</xsl:template>
	<xsl:template match="/">
		<html>
			<head>
				<title>Supplier Details</title>
			</head>
			<body>
				<h1>SUPPLIER DETAILS AT CYBERSHOP</h1>
				<table border="3" cellspacing="2" cellpadding="6">
					<thead align="center" bgcolor="silver">
						<th>ID</th>
						<th>Name</th>
						<th>Address</th>
						<th>Phone</th>
						<th>Product(s)</th>
					</thead>
					<tbody>
						<xsl:for-each select="supplier.details/supplier">
							<tr>
								<xsl:choose>
								<xsl:when test="position() mod 2 =0">	
									<td><font color="red"><xsl:value-of select="@sid"/></font></td>	
									<td><font color="red"><xsl:value-of select="@fname"/><xsl:text> </xsl:text><xsl:value-of select="@lname"/></font></td>
									<td><font color="red"><xsl:value-of select="address"/></font></td>
									<td><font color="red"><xsl:value-of select="phone"/></font></td>
									<td><font color="red"><xsl:apply-templates select="product"/></font></td>
								</xsl:when>
								<xsl:otherwise>
									<td><xsl:value-of select="@sid"/></td>	
									<td><xsl:value-of select="@fname"/><xsl:text> </xsl:text><xsl:value-of select="@lname"/></td>
									<td><xsl:value-of select="address"/></td>
									<td><xsl:value-of select="phone"/></td>
									<td><xsl:apply-templates select="product"/></td>															
								</xsl:otherwise>
								</xsl:choose>
							</tr>
						</xsl:for-each>
					</tbody>
				</table>

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


Here is the xml file:

<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="6p3.xsl"?>
<supplier.details>
	<supplier sid="S001" fname="Jason" lname="Smith">
		<address>10 Lafair Sr, NY 12034</address>
		<phone>123-456-7894</phone>
		<product>Office Stationary</product>
		<product>Furniture</product>
	</supplier>
	<supplier sid="S002" fname="Nathan" lname="Jessop">
		<address>10 Wellington St, MA 12034</address>
		<phone>348-279-8799</phone>
		<product>Water Dispenser</product>
		<product>Air Conditioning</product>
	</supplier>
	<supplier sid="S003" fname="Sam" lname="Hendrich">
		<address>45 1/2 Long BEach, CA 90210</address>
		<phone>478-939-2092</phone>
		<product>Intercom</product>
		<product>Telephone</product>
	</supplier>
</supplier.details>


I got proper output but did not understand how the xsl:apply-template element worked. I am supposed to get all the text in the product element into one field. Even if there are more than two or more product elements (in supplier) it prints the proper output (without no looping). How is this possible?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.