Subject: Re: [xsl] <xsl:sort> using a derived element From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Wed, 7 Mar 2001 18:12:07 +0000 |
Hi Edith, > Before I saw your reply, I saw Mike Kay's reply and did it a little > differently than what you had suggested below. Both work, is that > the case a lot of the time? Oh definitely - most problems have multiple solutions in XSLT. It's especially the case with xsl:for-each and xsl:apply-templates - they have very similar functionality. > This is a trivial combobox, but how should I decide which way to > implement something similar to this in the future? Well, in general I'd say that applying templates to a set of nodes is a more extensible way of doing something with them than iterating over them with xsl:for-each, so I think the solution that you came up with is better than the way that I gave on that score. In fact, it would be more in the XSLT tradition (if there can be said to be such a thing) to apply templates to the List: <xsl:apply-templates select="PATHFINDER/List[ListID = '102000']" /> then to have the template for the List provide the SELECT element that wraps round the OPTION elements you're creating: <xsl:template match="List"> <SELECT ...> <xsl:apply-templates select="Itm"> <xsl:sort select="substring-after(ItmDesc, '-')" /> </xsl:apply-templates> </SELECT> </xsl:template> [Note: this template matches any List element. The only List element that you're applying templates to is the one that's a child of PATHFINDER with a ListID equal to '102000' so there's no need to filter out any others with a longer match pattern here. The same kind of reasoning applies to the Itm-matching template below.] Then you could have a template matching Itm elements that provided the OPTION elements in the result: <xsl:template match="Itm"> <OPTION value="{ItmID}"> <xsl:value-of select="substring-after(ItmDesc, '-')" /> </OPTION> </xsl:template> [Note: in the above, I've used an attribute value template rather than xsl:attribute to create the 'value' attribute because it's less verbose and rather easier to read.] This is kinda elegant because basically the List element maps onto a SELECT element and the Itm element maps onto the OPTION element. Each template just defines the way that the particular element maps onto some HTML, and how (e.g. in what order) its contents should be processed. You can simply add more templates to deal with different kinds of Itm elements, if you want, or to deal with different kinds of List elements. The disadvantage is that it involves more templates. Each time you apply templates, the XSLT processor has to go off and find the template that should be processed, and that takes more time than simply giving the code there and then as you do in an xsl:for-each. So it might be slightly less efficient, but probably not so much that you'd generally notice. Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] <xsl:sort> using a derive, RUSSO, EDITH | Thread | [xsl] RE: Xalan-J2 outputing extra , Scott_Boag |
RE: [xsl] MSXML ancestor-or-self, Richard Mitchell | Date | Re: [xsl] [exsl] EXSLT 1.0 - Common, Jeni Tennison |
Month |