Re: [xsl] How to make a request to a same file to generate different content

Subject: Re: [xsl] How to make a request to a same file to generate different content
From: "cking" <cking@xxxxxxxxxx>
Date: Mon, 30 Aug 2004 19:06:36 +0200
Hi,

>College members (url = members.xml)
>      old members (url = members.xml)
>      new members (url = members.xml)

If I understand you correctly, your xml source holds a list of members, 
a number of which are "old" and some are "new" - maybe by having 
an attribute like <member type="old">...</member>, or by their 
position in the document, like

<members>
    <member>...</member>
    <member>...</member>
    <old>
        ... old members ...
    </old>
    <new>
        ... new members ...
    </new>
</members>

Now you could write three stylesheets: one that outputs all members,
one for the old members only, and one for the new members. Or you
could have one stylesheet with a stylesheet parameter:

<xsl:stylesheet version="1.0" ...>
    <xsl:param name="type" select="'default'">
    ...
    <xsl:choose>
        <xsl:when test="$type = 'new'">
            ...
        </xsl:when>
        <xsl:when test="$type = 'old'">
            ...
        </xsl:when>
        <xsl:otherwise>
            ...
        </xsl:otherwise>
    </xsl:choose>

The problem is, how to translate this into HTML hyperlinks. 
It's a problem in both cases (3 or 1 stylesheets), because typically, 
you supply the xml filename in the hyperlink, and the xsl stylesheet 
in a PI inside the xml source file:
<?xml-stylesheet type="text/xsl" href="members.xsl"?>

You'd want in your HTML something like:

<a href="members.xml">members</a>
<a href="members.xml?type=old">old members</a>
<a href="members.xml?type=new">new members</a>

Unfortunately, AFAIK there's no standard procedure for something like
this, because the XSLT specifications don't define the way how params 
are passed to the processor.  

You could do it with an MSXML script:
http://www.biglist.com/lists/xsl-list/archives/200308/msg00449.html
but this will only work in IE6. 

If you want something that also works in other browsers, you could make
two "dummy" xml files, each with their own stylesheet, something like:

--- members-old.xml ---
<?xml-stylesheet type="text/xsl" href="members-old.xsl"?>
<dummy/>

--- members-old.xsl ---
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:import href="members.xsl"/>
    <xsl:template match="/">
        <xsl:apply-templates select="document('members.xml')/members/old/member"/>
    </xsl:template>
</xsl:stylesheet>

--- members.xsl ---
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:template match="/members">
        <xsl:apply-templates select="member | old/member | new/member"/>
    </xsl:template>
    <xsl:template match="member">
        ... process the member elements ...
    </xsl:template>
</xsl:stylesheet>

And then your links would look like:

<a href="members.xml">members</a>
<a href="members-old.xml">old members</a>
<a href="members-new.xml">new members</a>

This will work in IE6 and the Gecko-type browsers (Mozilla, Firefox and Netscape I think)
but there are still other browsers that simply don't support client-side XSLT.

Another alternative might be taking the "one stylesheet with params" approach,
using JSP to extract the params from the hyperlink, and call a Java XSLT 
processor to do the transform and send the HTML output to the browser:

<a href="members.jsp">members</a>
<a href="members.jsp?type=old">old members</a>
<a href="members.jsp?type=new">new members</a>

(In this case, of course, you don't need the <?xml-stylesheet PI in the input file)

I hope this helps...

Best regards,
Anton Triest


Saturday, August 28, 2004 7:09 PM, IceT wrote:
>
> So I may have to use Cocoon for this, but I could certainly do it with 
> some server-side scripting (such as JSP) right ?
> 
> Is this a limitation of XSLT or it isn't meant to do this anyway? I 
> could for instance, create "empty" (with a root tag and a stylesheet 
> attached) files named members_new.xml and members_old.xml, and than make 
> the link point to this file. This is how I picture I could to this:
> 
> <xsl:variable name="current" select="/"/>
>     <xsl:choose>
>         <xsl:when test="current/* = 'members'>
>          ...
>         </xsl:when>
>         <xsl:when test="current/* = 'members_new'">
>         ...
>         </xsl:when>
>         <xsl:when test="current/* = 'members_old'">
>         ...
>         </xsl:when>
>     </xsl:choose>
> 
> Is this possible? Is there a better xml/xsl to do this?
> 
> Thanks
> 
> Jarno.Elovirta@xxxxxxxxx wrote:
> 
> >Hi,
> >
> >  
> >
> >>I have a menu bar with a few links. When I click on one of 
> >>them, a xml 
> >>file is called with its own stylesheet. Then appears a 
> >>submenu relative 
> >>to that menu item. When one os those subMenuItens are 
> >>clicked, the same 
> >>xml file is used to generate a different output:
> >>
> >>College members (url = members.xml)
> >>      old members (url = members.xml)
> >>      new members (url = members.xml)
> >>College buildings (url = buildings .xml)
> >>
> >>How can I do this? Using Cocoon I can create an elaborated 
> >>request and 
> >>then use it to select a suitable pipeline:
> >>
> >>College members (url = members.xml)
> >>      old members (url = members.xml:old)
> >>      new members (url = members.xml:new)
> >>College buildings (url = buildings .xml)
> >>
> >>How would I accomplish this using only xml/xsl ?
> >>    
> >>
> >
> >You don't with XSLT. Consult Cocoon users-list.
> >
> >Cheers,
> >
> >Jarno - Madam Zu: September 2002 Mix 

Current Thread