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
XML sorting; Converting from one XML to another XML Posted by biki on 1 Feb 2005 at 12:04 AM


Hi, I have this XML file of a simple collection of address books:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="address.xsl" ?>
<addressBooks>

<!-- diary 1  -->
<diary>
	<name>Friends</name>
	<!-- describe columns first, then put out the data in 'entry' instances. -->
	<columns>
		<name>Name</name>
		<name>Age</name>
	</columns>
	<entry>
		<rec>Record Number 1</rec>
		<value>Mary</value>
		<value>59</value>
	</entry>
	<entry>
		<rec>Record Number 2</rec>
		<value>Pankaj Kapur</value>
		<value>84</value>
	</entry>
	<entry>
		<rec>Record Number 3</rec>
		<value>John Jay</value>
		<value>14</value>
	</entry>
</diary>


<!-- diary 2  -->
<diary>
	<name>White Office</name>
	<!-- describe columns first, then put out the data in 'entry' instances. -->
	<columns>
		<name>Name</name>
		<name>Age</name>
	</columns>
	<entry>
		<rec>Record Number 1</rec>
		<value>Vice President</value>
		<value>21</value>
	</entry>
	<entry>
		<rec>Record Number 2</rec>
		<value>President</value>
		<value>4</value>
	</entry>
	<entry>
		<rec>Record Number 3</rec>
		<value>Janitor</value>
		<value>49</value>
	</entry>
	<entry>
		<rec>Record Number 4</rec>
		<value>Chef</value>
		<value>36</value>
	</entry>
</diary>



</addressBooks>


I use this XSL to generate a formatted HTML page:

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


<xsl:template match="/addressBooks">
		<xsl:apply-templates select="diary">
			<xsl:sort select="name"/> <!-- sorts diary name-->
		</xsl:apply-templates>
</xsl:template>

<xsl:template match="diary">
		<h3>
			<xsl:apply-templates select="name">
				<xsl:sort select="name"/> <!-- prints diary name-->
			</xsl:apply-templates>
		</h3>

	<table border="1" cellpadding="4">
		<tr bgcolor="lavender">
			<td bgcolor="pink">
			</td>
			<xsl:apply-templates select="columns">
				<xsl:sort select="name"/> <!-- prints metric name, sorted. name or columns/name doesnt seem to work, only the '.' does -->
			</xsl:apply-templates>
		</tr>

		<xsl:apply-templates select="entry">
			<xsl:sort select="value[2]" order="ascending"/>
			
			<!-- 
				value[1] : sorts the "Name" column in the Friends AND Office table
				value[2] : sorts the "Age" column ONLY in the Friends table
				value[3] : sorts the "Record Number" AND "Name" column in the Friends AND Office table
			
			
			-->
		</xsl:apply-templates>


	</table>
</xsl:template>

<xsl:template match="columns/name">
	<xsl:for-each select=".">
		<td>
			<b>
				<xsl:apply-templates/>
			</b>
		</td>
	</xsl:for-each>
</xsl:template>

<xsl:template match="entry">
	<tr>
		<xsl:for-each select="rec">
			<td bgcolor="wheat"><i><xsl:apply-templates/></i></td>
		</xsl:for-each>
		<xsl:for-each select="value">
			<td><xsl:apply-templates/></td>
		</xsl:for-each>
	</tr>
</xsl:template>

</xsl:stylesheet>


It works well, well mostly. The text in blue above just doesnt seem to work well. Whats wrong?

Also, is there a way to convert an XML file to another XML file?

For example, how to convert 
	from:

	<entry>
		<rec>Record Number 1</rec>
		<value>Vice President</value>
		<value>21</value>
	</entry>
	
	to:

	<entry>
		<rec>Record Number 1</rec>
		<name>Vice President</age>
		<age>21</age>
	</entry>


Howwwww??

Thanks in advance,
Bikram

~Bikram



 

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.