cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Verhage <pe...@ibuildings.nl>
Subject Choose: When / Otherwise
Date Wed, 13 Dec 2000 10:30:41 GMT
I'm trying to make a taglib which has a choose, when and otherwise tag
that work similar like their XSL equivalents.

It's no problem for me to rewrite the:
<taglib:choose>
  <taglib:when test="...">...</taglib:when>
  <taglib:when test="...">...</taglib:when>
  <taglib:otherwise>...</taglib:otherwise>
</taglib:choose>

To something like:
if (....)
{
  ...
}
else if (....)
{
  ...
}
else (....)
{
  ...
}

The thing I do have problems with is the test attribute. I want to make
it possible to test on certain request params in a way like this:
<taglib:when test="$view='list'">...</taglib:when>

So this would have to be replaced by:
if (request.getParameter("view") != null &&
request.getParameter("view").equals("list"))
{
 ...
}

I want something like this to be in the source code of the page it's
used in. Unfortunately I see no way to make such replacements with
simple pure XSLT. So this would mean I would have to parse this with
some self made JAVA. But as you can understand I would really like to
see the above instead of maybe 20 lines of code to parse it to do the
exact same thing. That's why I would like to parse it at the first time
it get's requested, and save the output directly in the repositories
.java file for the page this is used on.

Has someone done something similar like this and can provide me with
some nifty code, or does anyone have any suggestions to get the job
done? :) Maybe there is a way by just using XSLT. But I don't think XSLT
is capable of doing such things on it's own (it cannot replace text as
far as I know, it would have been pretty cool if it could).

BTW, I found a way I could use the test param to do this directly as the
above example, this was by doing the following:
<taglib:when test="request.getParameter(&quot;view&quot;) != null &&
request.getParameter(&quot;view&quot;).equals(&quot;list&quot;)">
...
</taglib:when>

Obviously, I don't want this. The taglib has to be made to be
transparent, and easy to use for whoever uses the taglib. And so this is
not the way I want to go.

Here is the code I have for the taglib so far, this is WITH the dirty
hack for the test variable:

<xsl:template match="taglib:choose">
  <xsl:apply-templates select="taglib:when|taglib:otherwise"/>
</xsl:template>

<xsl:template match="taglib:choose/taglib:when">
  <xsl:choose>
    <xsl:when test="position()>1">
      <xsp:logic>
        else if (<xsl:value-of select="@test"/>) 
        {
          <xsp:content>
            <xsl:apply-templates/>
          </xsp:content>
        }
      </xsp:logic>
    </xsl:when>
    <xsl:otherwise>
      <xsp:logic>
        if (<xsl:value-of select="@test"/>) 
        {
          <xsp:content>
            <xsl:apply-templates/>
          </xsp:content>
        }
      </xsp:logic>
    </xsl:otherwise>    
  </xsl:choose>
</xsl:template>

<xsl:template match="taglib:choose/taglib:otherwise">
  <xsp:logic>
    else
    {
    <xsp:content>
      <xsl:apply-templates/>
    </xsp:content>
    }
  </xsp:logic>
</xsl:template>


With best regards,

Peter

-- 
Peter Verhage       <peter@ibuildings.nl>
ibuildings.nl BV - information technology
http://www.ibuildings.nl -  0118 41 50 54

Mime
View raw message