Subject: [xsl] Dynamic Sorting - More parametric From: Cristobal Galiano Fernandez <c.galiano@xxxxx> Date: Tue, 03 Apr 2001 12:28:47 +0200 |
More parametric. I need use two or (generaly) more columnes to sort. I tented some ways but unsucessfully. Can any do that Cristóbal galiano Fdez <root> <sort> <col id="name" /> <col id="age" type="number" order="descending"/> </sort> <data> <person><name>Bob</name><age>21</age></person> <person><name>Van</name><age>1</age></person> <person><name>Jim</name><age>63</age></person> <person><name>Van</name><age>2</age></person> <person><name>Gus</name><age>55</age></person> <person><name>Van</name><age>18</age></person> <person><name>Van</name><age>4</age></person> <person><name>Sam</name><age>32</age></person> <person><name>Don</name><age>39</age></person> <person><name>Ron</name><age>33</age></person> <person><name>Van</name><age>10</age></person> </data> </root> "Diamond, Jason" escribió: > Hi, Mark. > > The select attribute doesn't allow attribute value templates. But if all > you're sorting by is the contents of a dynamically specified child element > then you can use the local-name function to match against your variable. > > <xsl:stylesheet version="1.0" > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> > > <xsl:output method="html" indent="no"/> > > <xsl:template match="/"> > <xsl:apply-templates/> > </xsl:template> > > <xsl:template match="root"> > <xsl:apply-templates select="data"> > <xsl:with-param name="sortexpression" select="sortby"/> > </xsl:apply-templates> > </xsl:template> > > <xsl:template match="data"> > <xsl:param name="sortexpression"/> > <xsl:apply-templates select="person"> > <xsl:sort select="*[local-name()=$sortexpression]"/> > </xsl:apply-templates> > </xsl:template> > > <xsl:template match="person"> > <xsl:value-of select="name"/> > <xsl:text>, </xsl:text> > <xsl:value-of select="age"/> > <br /> > </xsl:template> > > </xsl:stylesheet> > > All I changed was the select attribute on the sort element. > > Hope this helps, > Jason. > > > -----Original Message----- > > From: Mark Swardstrom [mailto:mark@xxxxxxxxxx] > > Sent: Monday, April 02, 2001 10:59 AM > > To: 'XSL-List@xxxxxxxxxxxxxxxxxxxxxx' > > Subject: [xsl] Dynamic Sorting - Using a variable to > > determine the sort > > select e xpression > > > > > > Hi - I'm trying to dynamically sort a list based on the value > > of an element > > in my xml document. Ideally, I'd be able to pass this value > > to the xsl:sort > > element in the xsl document to determine the sort criteria. > > In the example > > below, "sortby" could have the value "name" or "age". > > > > Here's the XML: > > > > <root> > > <sortby>name</sortby> > > <data> > > <person><name>Bob</name><age>21</age></person> > > <person><name>Jim</name><age>63</age></person> > > <person><name>Gus</name><age>55</age></person> > > <person><name>Van</name><age>18</age></person> > > <person><name>Sam</name><age>32</age></person> > > <person><name>Don</name><age>39</age></person> > > <person><name>Ron</name><age>33</age></person> > > </data> > > </root> > > > > > > And here's the XSL I tried (it doesn't work, but you'll see > > what I'm trying > > to do). I also tried curly braces in the xsl:sort select tag > > to evaluate > > the sortexpression string as an expression, but no luck. I understand > > that's not the intended use of the curly brackets, but gave it a shot. > > > > > > <xsl:stylesheet version="1.0" > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> > > > > <xsl:output method="html" indent="no"/> > > > > <xsl:template match="/"> > > <xsl:apply-templates/> > > </xsl:template> > > > > <xsl:template match="root"> > > <xsl:apply-templates select="data"> > > <xsl:with-param name="sortexpression" select="sortby"/> > > </xsl:apply-templates> > > </xsl:template> > > > > <xsl:template match="data"> > > <xsl:param name="sortexpression"/> > > <xsl:apply-templates select="person"> > > <xsl:sort select="$sortexpression"/> > > </xsl:apply-templates> > > </xsl:template> > > > > <xsl:template match="person"> > > <xsl:value-of select="name"/> > > <xsl:text>, </xsl:text> > > <xsl:value-of select="age"/> > > <br /> > > </xsl:template> > > > > </xsl:stylesheet> > > > > > > If I can't do this, I find I have to write a bunch of xsl:if > > clauses and > > that solution doesn't scale and results in bulky code. > > > > Thanks ahead of time for your thoughts. > > > > Mark Swardstrom > > Nimble Technology > > > > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > > > 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Dynamic Sorting - Using a, Diamond, Jason | Thread | RE: [xsl] Dynamic Sorting - More pa, Michael Kay |
RE: Relative paths in xsl:import Wa, Michael Kay | Date | RE: Q: Relative paths in xsl:import, Michael Kay |
Month |