Subject: Re: [xsl] How can I improve this From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Fri, 9 Nov 2001 10:18:30 +0000 |
Hi Walter, > Q: How can I pull out of the 'config' which item is to be default > sorted and insert the item in the above (or similar) code. I think that you want to sort the interactions by their child element whose name is the same as the first id attribute in the node set held in the $config variable. You're using this same pattern later in your stylesheet - you need to select *all* the child elements and test their names (from the name() function) against the value from the $config variable: <xsl:apply-templates> <xsl:sort select="*[name() = $config[1]]" /> </xsl:apply-templates> You probably want to get the values of the order attribute from your $config information as well. You can store the order in an attribute: <xsl:variable name="order"> <xsl:choose> <xsl:when test="$config[1]/../@sortOrder = 'A'"> <xsl:text>ascending</xsl:text> </xsl:when> <xsl:otherwise>descending</xsl:otherwise> </xsl:choose> </xsl:variable> and use that in an attribute value template in the order attribute of the xsl:sort: <xsl:apply-templates> <xsl:sort select="*[name() = $config[1]]" order="{$order}" /> </xsl:apply-templates> > The above 'apply-template' tag calls this template... "The above xsl:apply-templates instruction tells the XSLT processor to apply templates to the interaction elements; these elements match this template..." > <!-- 'interaction' NODE Template --> > <xsl:template match="interaction"> [snip] > </xsl:template> > > The 'apply-templates' defined here calls this template... "The xsl:apply-templates instruction tells the XSLT processor to apply templates to the children of the interaction elements; most of these elements match this template..." > <!-- general NODE Template --> > <xsl:template match="curr_dest | media_type | detailed_comment | contact > | resolution | disposition"> > <td valign="top" class='data'> > <xsl:value-of select="."/> > <xsl:text> </xsl:text> > </td> > </xsl:template> > > This leads me to my second question: > > Q: I would like to make this last Template a generic one. > Right now, it is explicitly defined to handle these NODEs. What you're saying is that you need to make the above template match any kind of element. This is why I rephrased the descriptions that you used about "calling" templates. You only call a template in XSLT when you call it by name with xsl:call-template. When you apply templates, you're applying templates to a set of nodes and the XSLT processor works out which one to apply based on the match patterns of the templates. The processor gathers together all the templates that match a node and picks the one that has the highest priority (which works out to be the one with the most explicit match pattern, most of the time). To get a template to match any element, you need to give it a match pattern of *: <xsl:template match="*"> <td valign="top" class='data'> <xsl:value-of select="."/> <xsl:text> </xsl:text> </td> </xsl:template> > I am trying to have a generic Template called from the main > 'apply-template', but... > > There is always a but! > > I have 2 other templates that are used in special cases. The XSLT processor always picks the template with highest priority. A template that specifies the element that it matches explicitly, such as: > <!-- timestamp NODE Template --> > <xsl:template match="timestamp"> [snip] > </xsl:template> will always be chosen in preference to a template that matches any element. So the timestamp template will apply to the timestamp element, and all the other elements will use the template that matches any element. The template above overrides the built in template for elements, which might cause problems. If so, try changing the template so that it only matches element children of interaction elements, and give it a low priority so that the timestamp template is still used in preference: <xsl:template match="interaction/*" priority="-1"> <td valign="top" class='data'> <xsl:value-of select="."/> <xsl:text> </xsl:text> </td> </xsl:template> I hope that's what you were after, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] How can I improve this, Walter Torres | Thread | Re: [xsl] Re: lookup-table thoughts, Tom Myers |
RE: [xsl] reliability of MSXML, Michael Kay | Date | Re: [xsl] Change an XSLT top-level , Jeni Tennison |
Month |