[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Question about xsl:apply-imports


Subject: Re: [xsl] Question about xsl:apply-imports
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 11 Sep 2009 11:39:54 -0400

Bill,

Unlike xsl:apply-templates, which unless you explicitly say otherwise, selects child nodes for processing, xsl:apply-imports applies the best available imported template *to the context node*.

What's happening here is that the "rm:associated" template is applying an imported template to the rm:associated element it has matched. If no other template is available, this will be the built-in template. When its rm:document child is matched, the best "rm:document" template is being used. In this case, the one in rendereddocument.xsl is winning over the one in fields.xsl.

The best way of achieving the effect you want depends on other factors. It might involve having a mode or naming a template. It looks to me as though the real difference may be between rm:document elements that are inside rm:associated, and those that are not, which suggests you might want to match them accordingly in rendereddocument.xsl:

<xsl:template match="rm:associated/rm:document">
  <!-- applying the template in fields.xsl: -->
  <xsl:apply-imports/>
</xsl:template>

I hope this helps,
Wendell

At 09:27 PM 9/10/2009, you wrote:
Hi all,

xsl:import is pretty awesome, let me say that right off. But it sometimes yields surprising results.

I have a stylesheet called rendereddocument.xsl that imports a number of other stylesheets using the xsl:import instructions:

<xsl:import href="inc/baseline.xsl"/>
<xsl:import href="inc/application-tabs.xsl"/>
<xsl:import href="inc/fields.xsl"/>
<xsl:import href="inc/search-criteria.xsl"/>
<xsl:import href="inc/search-fields.xsl"/>
<xsl:include href="../../override/xslt/customization.xsl"/>

in fields.xsl, I have a template defined as follows:

<xsl:template match="rm:document">
  <xsl:element name="div" namespace="http://www.w3.org/1999/xhtml">
    <xsl:attribute name="class">
      <xsl:text>document</xsl:text>
      <xsl:if test="position() mod 2 = 0">
        <xsl:text> alt</xsl:text>
      </xsl:if>
    </xsl:attribute>
    <xsl:apply-templates select="rm:fields"/>
  </xsl:element>
</xsl:template>

and in rendereddocument.xsl I have two templates that are defined as follows:

<xsl:template match="rm:document" priority="-0.1">
  <rm:content name="rendereddocument.document">
    <xsl:apply-templates select="rm:fields"/>
  </rm:content>

  <xsl:apply-templates select="rm:associated"/>
</xsl:template>

<xsl:template match="rm:associated" priority="-0.1">
<rm:content name="{concat('rendereddocument.associated.', ../rm:associated/@name)}">
<div class='component collapsible-component collapsed'>
<div class='component-title'>
<h3><a href='#'><xsl:value-of select="@displayName"/></a></h3>
<ul class='component-action-list'>
<li class='active'><a href='#'>Showing relevant</a></li>
<li class='last'><a href='#'>Show all</a></li>
</ul>
</div>
<div class='component-part component-document-list'>
<xsl:apply-imports/>
</div>
</div>
</rm:content>
</xsl:template>


In the last template, there's an xsl:imports instruction. The element on which this template is matching, rm:associated, has rm:document children. It's my understanding that xsl:apply-imports should elect to apply the xsl:template defined in fields.xsl, not in rendereddocument.xsl. However, this isn't what's happening. Instead, the template matching rm:document defined in rendereddocument.xsl is being applied.

Any help would be most appreciated. Let me know if you need to see all the files. Last time, the issue was glaringly apparent from just the first email. ;)

Best regards,

--Bill F.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
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
Keywords
xml