[xsl] RE: sorting using a precalculated value

Subject: [xsl] RE: sorting using a precalculated value
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 28 Mar 2001 10:36:32 -0800 (PST)
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: [xsl] 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: [xsl] 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