SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Override named xsl:templates in stylesheet

    Hi,

    Is it possible to override an <xsl:template name=""> element (i.e. in the same way a method in PHP can be overridden by a subclass)?

    Code XML:
    default.xsl:
    <xsl:template name="panel">
       <div style="color:red">...</div>
    </xsl:template>
     
    theme.xsl:
    <xsl:template name="panel">
       <div style="color:blue">...</div>
    </xsl:template>

    Calling <xsl:call-template name="panel" /> should now output a blue panel rather than a red one. This is for a CMS theme system where the user's theme can optionally override predefined templates.

    (I am using the PHP5 XML parser, which throws a compilation error when parsing a template with the same name)

    Thanks,
    Chris
    Last edited by cbl2001; Oct 1, 2008 at 23:44. Reason: Added parser info

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you can't override a template in xsl, and that's why you have this error.
    You will need another mechanism, like setting a xsl:variable that specify if a theme should be used, and use an xsl:choose construction to do a call template.
    Code:
    <xsl:variable name="Theme">1</xsl:variable>
    ...
    <xsl:choose>
      <xsl:when test="$Theme=1">
        <xsl:call-template name="theme_panel"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="default_panel"/>
      </xsl:otherwise>
    </xsl:choose>
    But doing this, you loose the flexibility of apply-templates.

    Another solution, that I retained when I did this: http://www.quixml.org, is to have separate folder with a set of xsl style sheets, and having a theme variable that switch the source folder of the xsl stylesheets.
    It's what I've called "rendering sets", in my quixml class.

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your quick reply; your quixml engine looks an interesting solution to the problem.

    I also started wondering if, rather than explicitly checking for the theme, I could use the <xsl:apply-templates /> rather than <xsl:call-templates /> and replace the name attribute with mode, i.e.

    Code XML:
    <xsl:template select="*" mode="panel">
       ...
    </xsl:template>
     
    <xsl:apply-templates select="." mode="panel" />

    As the parser seems to allow overriding templates with the same mode attribute. Would this be likely to cause any problems? My site's themes are complicated as they can be mixed together at any point, so explicitly checking for themes may not always be appropriate.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I honestly don't know...
    I never used the mode attribute, but I'm not sure it will allow it:
    http://www.zvon.org/xxl/XSLTreferenc...ates_mode.html
    Both xsl:template and xsl:apply-templates have an optional mode attribute. If xsl:template does not have a match attribute, it must not have a mode attribute. If an xsl:apply-templates element has a mode attribute, then it applies only to those template rules from xsl:template elements that have a mode attribute with the same value; if an xsl:apply-templates element does not have a mode attribute, then it applies only to those template rules from xsl:template elements that do not have a mode attribute.
    And it example:
    http://www.zvon.org/xxl/XSLTreferenc...ode_frame.html
    lead me to think that if you have several xsl templates of the same name with the same mode, you will have the same error.
    Which seems logic to me, as this is the case with the default mode.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •