You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Ulrich Mayring <ul...@denic.de> on 2000/07/03 18:12:08 UTC

How to do logic in stylesheets?

Hello,

How can I execute some logic (XSP taglibs or such) within a stylesheet?
I have an XSLT stylesheet, which does a <xsl:choose> and in each of the
<xsl:when> branches it needs to execute different logic.

The obvious answer is: write the if-then branching in Java using XSP.
The thing is, however, that I need to do the XSLT transform first,
otherwise I don't know where to branch to under the various conditions.
And I don't want to write the whole XSLT transform in Java either,
because I have cocoon for this job :)

Basically, I want something like:

<?cocoon-process type="xslt"?>
<?cocoon-process type="xsp"?>

But I think this is not possible, is it?

Ulrich

-- 
Ulrich Mayring
DENIC eG, Systementwicklung

Re: How to do logic in stylesheets?

Posted by Uli Mayring <ul...@denic.de>.
On Mon, 3 Jul 2000, Donald Ball wrote:

> Note that for this sort of application you are almost certainly better
> off with your first XSLT transformation being applied as a logicsheet.
> It's faster and eliminates the caching problem. I can post an example for
> the curious.

Please do. Although I haven't yet checked out Kevin's code (I'm at home
now), I think that I have tried his approach and it didn't work. Basically
I generated a dynamic XSP page with a stylesheet, using a
<xsl:processing-instruction> thing. I posted a bug report on Friday
showcasing my solution and demonstrating what seems to be "ye old cache
problem".

So, if you have a solution, I'd be very grateful if you posted it,

Ulrich

-- 
Ulrich Mayring
DENIC eG, Softwareentwicklung


Re: How to do logic in stylesheets?

Posted by Donald Ball <ba...@webslingerZ.com>.
On Mon, 3 Jul 2000, Kevin Sonney wrote:

> On Mon, 3 Jul 2000, Ulrich Mayring wrote:
> > Basically, I want something like:
> > 
> > <?cocoon-process type="xslt"?>
> > <?cocoon-process type="xsp"?>
> > 
> > But I think this is not possible, is it?
> 
> Sure it is. I do it a lot. But you have to do something like the
> examples. below. Note : All server names & special queries have been
> changed or removed to protect the innocent. Props to Donald Ball for doing
> this first. Cacheing *CAN* be a problem, but there are hacks to make
> Cocoon re-gen the xsp page nearly every time.

Note that for this sort of application you are almost certainly better
off with your first XSLT transformation being applied as a logicsheet.
It's faster and eliminates the caching problem. I can post an example for
the curious.

- donald


Re: How to do logic in stylesheets?

Posted by Kevin Sonney <ke...@webslingerz.com>.
On Mon, 3 Jul 2000, Ulrich Mayring wrote:
> Basically, I want something like:
> 
> <?cocoon-process type="xslt"?>
> <?cocoon-process type="xsp"?>
> 
> But I think this is not possible, is it?

Sure it is. I do it a lot. But you have to do something like the
examples. below. Note : All server names & special queries have been
changed or removed to protect the innocent. Props to Donald Ball for doing
this first. Cacheing *CAN* be a problem, but there are hacks to make
Cocoon re-gen the xsp page nearly every time.

<!-- Begin test.xml file -->
<?xml version="1.0"?>
<?xml-stylesheet href="wZ.xsl" type="text/xsl"?>

<?cocoon-process type="xslt"?>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>

<xsp:page 
	language="java"
	xmlns:xsp="http://www.apache.org/1999/XSP/Core"
	xmlns:sql="http://www.apache.org/1999/SQL"
	xmlns:wZ="http://www.webslingerZ.com/XML"
>

<page>
<!-- Specify the final xsl file -->
<wZ:stylesheet href="test.xsl" />

 <ldap-defs>
  <ldap-server name="unc-cas">
   <initializer>com.sun.jndi.ldap.LdapCtxFactory</initializer>
	<ldap-serverurl>ldap://some.ldap.server</ldap-serverurl>
  </ldap-server>
 </ldap-defs>

 <ldap-query server="unc-cas" ldap-searchbase="o=webslingerZ,c=US" doc-element="wZ:user" row-element="wZ:ldapResult">
 sn=Sonney* 
 </ldap-query>
</page>
</xsp:page>
<!-- End test.xml -->

<!--Begin wZ.xsl -->
<?xml version="1.0"?>

<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:sql="http://www.apache.org/1999/SQL"
	xmlns:wZ="http://www.webslingerZ.com/XML"
version="1.0">

<!-- Remember that xsl file? This is where we build the pi for it. -->
<xsl:template match="/">
	<xsl:if test="//wZ:stylesheet">
		<xsl:processing-instruction name="xml-stylesheet">type=&quot;text/xsl&quot; href=&quot;<xsl:value-of select="//wZ:stylesheet/@href"/>&quot;</xsl:processing-instruction>
	</xsl:if>
	<xsl:apply-templates/>
</xsl:template>


<!-- settings to copy the unaffected tags --> 
<xsl:template match="@*|node()" priority="-1">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<!-- get the user data -->
<xsl:template match="wZ:user">
	<xsl:for-each select="wZ:ldapResult">
	<sql:execute-query>
	<!-- some query here dependant on ldap data -->
	</sql:execute-query>
	</xsl:for-each>
</xsl:template>

<xsl:template match="wZ:stylesheet"></xsl:template>

<!-- rebuild remaining PIs -->
<xsl:template match="processing-instruction()">
	<xsl:if test="not(name(.)='xml-stylesheet')">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
	</xsl:if>
</xsl:template>
</xsl:stylesheet>
<!-- End wZ.xsl -->

<!-- begin test.xsl -->
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="page">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"/>
  <title>Test Page</title>
 </head>
 <body>
	<table>
		<tr><th>Name</th><th>Address</th></tr>
		<xsl:apply-templates />
	</table>	
 </body>
</html>
</xsl:template>

<xsl:template match="rowset">
		<xsl:for-each select="row">
			<xsl:sort select="name"/>
				<tr><td><xsl:value-of select="name"/></td>
				<td><xsl:value-of select="address"/></td></tr>
		</xsl:for-each>
</xsl:template>


</xsl:stylesheet>
<!-- End test.xsl -->
-- 
- Kevin Sonney
  kevin@webslingerZ.com