[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] XSLT Problem - Random Order


Subject: RE: [xsl] XSLT Problem - Random Order
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Mon, 6 Oct 2003 19:59:43 +0100

> But I want to select the category at random order not
> as ascending or descending order. 

You're not being very clear. I suspect that when you say "random" order
you actually mean "arbitrary" order - i.e. you want to control the
sequence from your stylesheet, rather than having the elements displayed
in an unpredictable sequence.

The simplest way to do this is with something like:

<xsl:apply-templates select="//book[@category='c4']"/>
<xsl:apply-templates select="//book[@category='c2']"/>
<xsl:apply-templates select="//book[@category='c1']"/>
<xsl:apply-templates select="//book[@category='c5']"/>
<xsl:apply-templates select="//book[@category='c3']"/>

Or you could use keys to make it more efficient.

Micahel Kay

> Suppose there are 6 categories in 
> 
> books.xml file. Now I want to select only 3 categories
> in ouput file ( e.g in html) and their position will
> be different - 
> 
> such as 4th (category="c4" in xml doc) will come at
> 1st position (in html doc), 6th (category="c6" in xml
> doc) will come at 
> 
> 2nd position (in html doc) and 2nd (category="c2" in
> xml doc) will come at 3rd position(in html doc).
> 
> 
> books.xml
> ----------
> 
> <?xml version="1.0"?>
> <books>
> 	<book category="c1">
> 		<title>1st Book</title>
> 		<auth>Author 1</auth>
> 	</book>
> 
> 	<book category="c2">
> 		<title>2nd Book</title>
> 		<auth>Author 2</auth>
> 	</book>
> 	<book category="c3">
> 		<title>3rd Book</title>
> 		<auth>Author 3</auth>
> 	</book>
> 	<book category="c4">
> 		<title>4th Book</title>
> 		<auth>Author 4</auth>
> 	</book>
> 	<book category="c5">
> 		<title>5th Book</title>
> 		<auth>Author 5</auth>
> 	</book>
> 	<book category="c6">
> 		<title>6th Book</title>
> 		<auth>Author 6</auth>
> 	</book>
> </books>
> 
> 
> 
> books.xsl
> ----------
> 
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> 
> 	<xsl:template match="/">
> 		<html>
> 			<head>
> 			</head>
> 			<body>
> 				<table border="1" width="60%" 
> align="center" cellpadding="0">
> 					<xsl:apply-templates/>
> 				</table>
> 			</body>
> 		</html>
> 	</xsl:template>
> 
> 
> 	<xsl:template match="books">
> 		<xsl:for-each select="//book">
> 
> 
> 			<xsl:choose>
> 
> 				<xsl:when test="@category='c4'">
> 
> 					<tr>
> 						<td align="center">
> 							
> <xsl:value-of select="title"/>
> 						</td>
> 						<td align="center">
> 							
> <xsl:value-of select="auth"/>
> 						</td>
> 					</tr>
> 				</xsl:when>
> 
> 
> 				<xsl:when test="@category='c6'">
> 
> 					<tr>
> 						<td align="center">
> 							
> <xsl:value-of select="title"/>
> 						</td>
> 						<td align="center">
> 							
> <xsl:value-of select="auth"/>
> 						</td>
> 					</tr>
> 				</xsl:when>
> 
> 
> 				<xsl:when test="@category='c2'">
> 					<tr>
> 						<td align="center">
> 							
> <xsl:value-of select="title"/>
> 						</td>
> 						<td align="center">
> 							
> <xsl:value-of select="auth"/>
> 						</td>
> 					</tr>
> 				</xsl:when>
> 			</xsl:choose>
> 		</xsl:for-each>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> 
> 
> books.html
> ----------
> 
> Current Output
> --------------
> 
> 
> Category		Author
> --------		-------
> 
> 2nd Book		Author 2
> 
> 4th Book		Author 4
> 
> 6th Book		Author 6
> 
> 
> Expected Output 
> ---------------
> 
> 
> Category		Author
> --------		-------
> 
> 4th Book		Author 4
> 
> 6th Book		Author 6
> 
> 2nd Book		Author 2
> 
> 
> 
> Regards,
> 
> Saurabh Sinha
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  --- Michael Kay <mhk@xxxxxxxxx> wrote: > You haven't
> said what criteria you want to use for
> > sorting - is it
> > descending order by category name, or what?
> > 
> > You could get the order c3, c2, c1, by adding
> > 
> > <xsl:sort select="@category" order="descending"/>
> > 
> > immediately after the <xsl:for-each>
> > 
> > You have an xsl:choose with three branches, but they
> > are all the same.
> > This makes it very difficult to understand what
> > exactly you are trying
> > to achieve.
> > 
> > Michael Kay
> > 
> > 
> > > -----Original Message-----
> > > From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > > [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On
> > Behalf Of
> > > Saurabh Sinha
> > > Sent: 06 October 2003 08:13
> > > To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> > > Subject: [xsl] XSLT Problem - Random Order
> > > 
> > > 
> > > Hi,
> > > 
> > > Currently I am facing the following problem while converting 
> > > documents into HTML from XML. I need
> > help
> > > to solve this.
> > > 
> > > 
> > > Thanks,
> > > 
> > > Saurabh
> > > 
> > > 
> > > Problem:
> > > 
> > > I want to see the output of books.xml file as
> > category
> > > wise. In books.xml 3 categories are given c1, c2,
> > c3
> > > within book elements. I want to see either c3
> > first or
> > > c2 first in html output. But everytime it is
> > coming as
> > > per .xml document. I am writing the books.xml,
> > > books.xsl below. Can you suggest me what specific
> > > command I have to give in .xsl document.
> > > 
> > > 
> > > books.xml
> > > ----------
> > > 
> > > <?xml version="1.0"?>
> > > <books>
> > > 	<book category="c1">
> > > 		<title>ABC</title>
> > > 		<auth>XX</auth>
> > > 	</book>
> > > 
> > > 	<book category="c2">
> > > 		<title>SSS</title>
> > > 		<auth>YY</auth>
> > > 	</book>
> > > 	<book category="c3">
> > > 		<title>MNB</title>
> > > 		<auth>ZZ</auth>
> > > 	</book>
> > > </books>
> > > 
> > > 
> > > books.xsl
> > > ------------
> > > 
> > > <?xml version="1.0"?>
> > > <xsl:stylesheet version="1.0"
> > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> > > 
> > > 	<xsl:template match="/">
> > > 		<html>
> > > 			<head>
> > > 			</head>
> > > 			<body>
> > > 				<table border="1" width="60%"
> > > align="center" cellpadding="0">
> > > 					<xsl:apply-templates/>
> > > 				</table>
> > > 			</body>
> > > 		</html>
> > > 	</xsl:template>
> > > 
> > > 
> > > 	<xsl:template match="books">
> > > 		<xsl:for-each select="//book">
> > > 
> > > 			<xsl:choose>
> > > 
> > > 				<xsl:when test="@category='c3'">
> > > 
> > > 					<tr>
> > > 						<td align="center">
> > > 							
> > > <xsl:value-of select="title"/>
> > > 						</td>
> > > 						<td align="center">
> > > 							
> > > <xsl:value-of select="auth"/>
> > > 						</td>
> > > 					</tr>
> > > 				</xsl:when>
> > > 
> > > 
> > > 				<xsl:when test="@category='c1'">
> > > 
> > > 					<tr>
> > > 						<td align="center">
> > > 							
> > > <xsl:value-of select="title"/>
> > > 						</td>
> > > 						<td align="center">
> > > 							
> > > <xsl:value-of select="auth"/>
> > > 						</td>
> > > 					</tr>
> > > 				</xsl:when>
> > > 
> > > 					
> > > 					<xsl:when test="@category='c2'">
> > > 					<tr>
> > > 						<td align="center">
> > > 							
> > > <xsl:value-of select="title"/>
> > > 						</td>
> > > 						<td align="center">
> > > 							
> > > <xsl:value-of select="auth"/>
> > > 						</td>
> > > 					</tr>
> > > 				</xsl:when>
> > > 
> > > 			</xsl:choose>
> > > 		</xsl:for-each>
> > > 	</xsl:template>
> > > </xsl:stylesheet>
> > > 
> > > 
> > > 
> > > 
> > > books.html
> > > ----------
> > > 
> > > Current Output
> > > --------------
> > > 
> > > 
> > > Category		Author
> > > --------		-------
> > > 
> > > ABC			XX
> > > 
> > > SSS			YY
> > > 
> > > MNB			ZZ
> > > 
> > > 
> > > Expected Output
> > > ---------------
> > > 
> > > Category		Author
> > > --------		-------
> > > 
> > > MNB			ZZ
> > > 
> > > ABC			XX
> > > 
> > > SSS			YY
> > > 
> > > 
> > >
> >
> ______________________________________________________________
> > > __________
> > > Yahoo! India Matrimony: Find your partner online.
> > > Go to http://yahoo.shaadi.com
> > > 
> > >  XSL-List info and archive:
> > http://www.mulberrytech.com/xsl/xsl-list
> > > 
> > 
> > 
> >  XSL-List info and archive:
> > http://www.mulberrytech.com/xsl/xsl-list
> >  
> 
> ______________________________________________________________
> __________
> Yahoo! India Matrimony: Find your partner online.
> Go to http://yahoo.shaadi.com
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords