Re: [xsl] Conditional use of <xsl:apply-imports> or <xsl:import> XSLT 2.0

Subject: Re: [xsl] Conditional use of <xsl:apply-imports> or <xsl:import> XSLT 2.0
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Jul 2008 10:58:11 -0400

At 09:55 AM 7/11/2008, you wrote:
<xsl:template match="article/meta/journalcode[.='ABCD' | 'EFGH' | 'IJKL' |
<xsl:apply-imports/> <!--  Some thing I need to do here  -->

Another way of get it done is use of "mode" attribute <xsl:apply-templates
mode="Reference-style-1"/>, which infact I was using as of now, but I ending
up defining a huge stylesheet. This was reason I thought of having different
modules to make the things manageable.

My questions:

1. Can the conditional apply-imports can be done.

Yes. Although not with code such as you posted. The value of the @match pattern there is not a correct pattern. A better pattern for you to use might be:

(XSLT 2.0)
<xsl:template match="article/meta/journalcode

(XSLT 1.0)
<xsl:template match="article/meta/journalcode
  [.='ABCD' or .='EFGH' or .='IJKL' or .='LMNO']">

or you could engineer a conditional using xsl:if or xsl:choose/when/otherwise, which could be more verbose but is sometimes easier to maintain.

 Or even conditional import
of <xsl:import> instead of declaring all of them on top?

No, you can't do this. Importing is a compile-time operation, and can't be done dynamically. (This would be analogous to buying a tool at the hardware store that was either a hammer or a saw depending on what you wanted to do with it. You might be able to buy a tool that was both a hammer and a saw; but in that case even if you were using the hammer part, the saw part would still be there.)

2. I have also noticed that my transformation taking much more time after
declaring (xsl:import) all the stylesheet on top. Do this have anything to
do with importing lots of stylesheet or this my incorrect clairvoyance? I am
using XMLSPY enterprise edition.

It's impossible to know for sure at a distance (clairvoyance is like that), but this is highly likely and not uncommon, especially with many modules.

3. Finally, is this a correct method to approach?

It does not appear on the surface to be incorrect. On the other hand, you haven't provided many particulars.

On the other hand, I am not completely convinced that you don't want to to use modes. You can have families of templates in different modes living in separate stylesheet modules, to make them easier to maintain. Either templates with modes or named templates with parameters are more flexible than relying on xsl:apply-imports alone, even with conditional logic.


Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread