Re: [xsl] How can I improve this

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:sort select="*[name() = $config[1]]" />

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:when test="$config[1]/../@sortOrder = 'A'">

and use that in an attribute value template in the order attribute of
the xsl:sort:

      <xsl:sort select="*[name() = $config[1]]" order="{$order}" />

> 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

>    <!-- 'interaction' NODE Template -->
>    <xsl:template match="interaction">
>    </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>&#160;</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

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="."/>

>      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

>    <!-- timestamp NODE Template -->
>    <xsl:template match="timestamp">
>    </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="."/>

I hope that's what you were after,


Jeni Tennison

 XSL-List info and archive:

Current Thread