[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: sorting using a precalculated value

Subject: RE: sorting using a precalculated value
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 28 Mar 2001 10:36:32 -0800 (PST)
xslt class value select
Here's a somewhat simpler solution:

xml source document:
-------------------
<classes>
 <class name="OuterClass1">
   <class name="InnerClass">
     <class name="InnerInerClass"/>
   </class>
   <class name="InnerzClass">
     <class name="Inner2InerClass2"/>
   </class>
 </class>
 <class name="OuterClass2">
   <class name="InnerClass">
     <class name="InnerInerClass"/>
   </class>
   <class name="InnerzClass">
     <class name="Inner2InerClass2"/>
   </class>
 </class>
 <class name="AOuterClass"/>
</classes>

xslt stylesheet:
---------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method = "text" />
	<xsl:strip-space elements="*"/>
	<xsl:template match="classes">
		<xsl:apply-templates>
			<xsl:sort select="@name"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template match="class">
    <xsl:for-each select="ancestor-or-self::class">
      <xsl:value-of select="@name"/>
      <xsl:if test="not(position()=last())">.</xsl:if>
    </xsl:for-each>
    <xsl:text>&#xA;</xsl:text>
		<xsl:apply-templates>
			<xsl:sort select="@name"/>
		</xsl:apply-templates>

	</xsl:template>
</xsl:stylesheet>


Result:
------
AOuterClass
OuterClass1
OuterClass1.InnerClass
OuterClass1.InnerClass.InnerInerClass
OuterClass1.InnerzClass
OuterClass1.InnerzClass.Inner2InerClass2
OuterClass2
OuterClass2.InnerClass
OuterClass2.InnerClass.InnerInerClass
OuterClass2.InnerzClass
OuterClass2.InnerzClass.Inner2InerClass2


Hope this helped.

Cheers,
Dimitre Novatchev.


Stephane Bailliez <sbailliez at imediation dot com> wrote:

Nice.

I did not think about doing it this way.

I think I will use the temporary tree solution anyway because I need to do
this in several contexts, so I'd better navigate into this temporary tree. I
will try to put a few keys here and there at the same time to ease my job of
grouping as well.

Thanks for your input Chris.


-- 
 Stéphane Bailliez 
 Software Engineer, Paris - France 
 iMediation - http://www.imediation.com 
 Disclaimer: All the opinions expressed above are mine and not those from my
company. 



> -----Original Message-----
> From: Chris Bayes [mailto:Chris@xxxxxxxxxxx]
> Sent: Wednesday, March 28, 2001 5:39 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE:  sorting using a precalculated value
> 
> 
> Stephane,
> This will give you the desired output
> 
> <?xml version="1.0" encoding="utf-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 	<xsl:output method = "text" />
> 	<xsl:template match="classes">
> 		<xsl:apply-templates>
> 			<xsl:sort select="@name"/>
> 			<xsl:with-param name="name" select="@name" />
> 		</xsl:apply-templates>
> 	</xsl:template>
> 	<xsl:template match="class">
> 		<xsl:param name="name" />
> 		<xsl:choose>
> 			<xsl:when test="parent::class">
> 				<xsl:value-of 
> select="concat($name, '.', @name)"
> /><xsl:text>&#10;</xsl:text>
> 			</xsl:when>
> 			<xsl:otherwise>
> 			 	<xsl:value-of select="@name" 
> /><xsl:text>&#10;</xsl:text>
> 			</xsl:otherwise>
> 		</xsl:choose>
> 		<xsl:apply-templates>
> 			<xsl:sort select="@name"/>
> 			<xsl:with-param name="name">
> 				<xsl:choose>
> 					<xsl:when test="parent::class">
> 						<xsl:value-of 
> select="concat($name, '.', @name)" />
> 					</xsl:when>
> 					<xsl:otherwise>
> 					 	<xsl:value-of 
> select="@name" />
> 					</xsl:otherwise>
> 				</xsl:choose>
> 			</xsl:with-param>
> 		</xsl:apply-templates>
> 	</xsl:template>
> </xsl:stylesheet>
> 
> Wether it will fit in with what you are trying to do is 
> another matter.
> 
> Ciao Chris
> 
> XML/XSL Portal
> http://www.bayes.co.uk/xml
> 
> 
> >-----Original Message-----
> >From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Stephane
> >Bailliez
> >Sent: 28 March 2001 15:11
> >To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >Subject:  sorting using a precalculated value
> >
> >
> >I have the following XML:
> >
> ><classes>
> > <class name="OuterClass1">
> >   <class name="InnerClass"/>
> > </class>
> > <class name="AOuterClass"/>
> ></classes>
> >
> >I want to have the following output (ie, class name must be 
> built and the
> >whole stuff sorted)
> >
> >AOuterClass
> >OuterClass1
> >OuterClass1.InnerClass
> >
> >to build the class name I'm using:
> >
> ><xsl:template match="class" mode="class.name">
> > <xsl:if test="parent::class">
> >  <xsl:apply-templates select="parent::class" mode="class.name"/>
> >  .<xsl:value-of select="@name"/>
> > </xsl:if>
> > <xsl:if test="not(parent::class)">
> >  <xsl:value-of select="@name"/>
> > </xsl:if>
> ></xsl:template>
> >
> >However, I'm a little bit stuck here because I cannot do the 
> following:
> >
> ><xsl:templates match="classes">
> > <xsl:for-each select=".//class">
> >  <xsl:variable name="class.name>
> >   <xsl:apply-templates select="." mode="class.name"/>
> >  </xsl:variable>
> >  <xsl:sort select="$class.name"/> <!---- Not possible ----->
> >  <xsl:apply-templates select="." mode="class.name"/>
> ></xsl:template>
> >
> >Since I need to do this sorting/name resolution many times, 
> in different
> >contexts, it would be nice to precalculate all this via 
> keys, but I'm not
> >sure I can do this.
> >
> >If someone has an idea how to do this, I'd be glad to know. 
> I'm pretty sure
> >it's simple, I'm simply missing something.
> >
> >Thanks a lot.
> >



__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/?.refer=text

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.