cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J.Pietschmann" <j3322...@yahoo.de>
Subject Re: Different stylesheets called on runtime?
Date Thu, 23 Jan 2003 22:15:51 GMT
Johannes.Becker@o2.com wrote:
> My XSP, that should choose which XSL to use.:

Another possibility is to use *one* style sheet containing
all the templates and match on the document element to choose
the processing path.
For example, you want to use 2 style sheets:
1.xsl
<xsl:stylesheet ...>
   <xsl:template match="foo">
    ...
</xsl:stylesheet>
2.xsl
<xsl:stylesheet ...>
   <xsl:template match="bar">
    ...
</xsl:stylesheet>

First, add a mode to all templates to avoid name clashes, and
perhaps prefixes to global variables and parameters:
1.xsl
<xsl:stylesheet ...>
   <xsl:template match="foo" mode="xsl1>
    ...
</xsl:stylesheet>
2.xsl
<xsl:stylesheet ...>
   <xsl:template match="bar" mode="xsl2">
    ...
</xsl:stylesheet>

Make a stylesheet including all of the above and a document
element matching template to choose the further processing
path:
all.xsl:
<xsl:stylesheet ...>
   <xsl:include href="1.xsl"/>
   <xsl:include href="2.xsl"/>
   <xsl:template match="/xsl1">
     <xsl:apply-templates mode="xsl1"/>
   </xsl:template>
   <xsl:template match="/xsl2">
     <xsl:apply-templates mode="xsl2"/>
   </xsl:template>
</xsl:stylesheet>

Your XSP has simply to provide the appropriate document element.
I have absolutely no experience with XSP so I have to guess...
<xsp:page language="java"...>
       <!-- Get the passed parameters (if there are any) -->
       <xsp:logic>

      int whichXSL;
...
     // Now I want to choose the suitable XSL
     if( whichXSL == 1)
     </xsp:logic>
       <xsl1>
        <results>
           logic to put data here
        </results>
       </xsl1>
     <xsp:logic>
     else if( whichXSL == 2)
     </xsp:logic>
       <xsl2>
        <results>
           logic to put data here
        </results>
       </xsl2>
     <xsp:logic>
   ...

You'll have to rearrange your data generation a bit, of course.
If you don't want to do this, use a "marker element"
<xsp:page language="java"...>
   <!-- Get the passed parameters (if there are any) -->
    <results>
       <xsp:logic>

      int whichXSL;
...
     // Now I want to choose the suitable XSL
     if( whichXSL == 1)
     </xsp:logic>
       <xsl1/>
     <xsp:logic>
     else if( whichXSL == 2)
     </xsp:logic>
       <xsl2/>
     <xsp:logic>

and in the XSL
   <xsl:template match="/">
     <xsl:apply-templates select=/results/*[1]"/>
   </xsl:template>
   <xsl:template match="/results/xsl1">
     <xsl:apply-templates mode="xsl1"/>
   </xsl:template>
   <xsl:template match="/results/xsl2">
     <xsl:apply-templates mode="xsl2"/>
   </xsl:template>

The "marker element" must be the first child ot the results
element. There are ways to get around this, for example using
namespaces and matching
    <xsl:apply-templates select=/results/*[namespace-uri()='uri:foo']"/>

HTH
J.Pietschmann


---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail:   <cocoon-users-help@xml.apache.org>


Mime
View raw message