[xsl] Fwd: Dynamic template select criteria; params & variables

Subject: [xsl] Fwd: Dynamic template select criteria; params & variables
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 21 Aug 2002 14:44:43 +0100
[Forwarded on behalf of John Potter, who's having trouble posting to
 the list]

-----------------------------------

Dynamic template select criteria; params & variables

OK - After searching around a bit more, I've figured out that you're not
allowed to create dynamic XPath expressions but that dynamic predicates are
allowed...

Can anyone tell me why (from the example I submitted earlier) this isn't
working for me (no error but I get back all items whether they match the
criteria or not.

***************************

<xsl:variable name="test" select="'contains($color_criteria, @color) and
contains($letter_criteria, @letter)'"/>

<xsl:apply-templates select="records/record[$test]"/>

***************************

Hardcoding the entire select clause works as I would think it should...

<xsl:apply-templates 
                select="records/record[contains($color_criteria, @color) and
contains($letter_criteria, @letter)]"/>

This has got to be a fairly routine task.
                
Thanks.

John Potter

-----Original Message-----
From: John Potter 
Sent: Monday, August 19, 2002 2:09 PM
To: 'xsl-list@xxxxxxxxxxxxxxxx'
Subject: Dynamic template select criteria; params & variables


Hello:

I've been playing around with XSLT for the past couple months with good
results but have run into something that I'm having trouble getting around.

Essentially, I want to be able to pass in several values as parameters and
filter my output based on those values.

My initial thought was to create a variable, check the parameters one by one
and build the select criteria (specifically the predicates).  The content of
that variable would get placed in the apply-templates statement (e.g.
<xsl:apply-template select="$main_select_clause"/>).

I've got two problems - building the contents of the variable (I know - it's
more like a constant) and then getting the apply-template to work w/ the
variable (even if I build a simple variable w/ hard-coded values).

Am I missing something obvious?

You'll find a simplified example of what I'm trying to accomplish below.

Thanks.

John Potter

****************************************************************
Parser Info

Vendor: Microsoft
Vendor URL: http://www.microsoft.com

****************************************************************
xml file

<?xml version="1.0"?>

<records>
        <record num="1" letter="A" color="blue"/>
        <record num="2" letter="B" color="blue"/>
        <record num="3" letter="C" color="green"/>
        <record num="4" letter="D" color="yellow"/>
</records>

****************************************************************
xslt file

<?xml version="1.0"?>

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

        <xsl:output method="html"/>

        <xsl:param name="letter_criteria" select="'B'"/>
        <xsl:param name="color_criteria" select="'blue'"/>

        <xsl:variable name="main_select_clause"/>

<xsl:template match="/">
        <html>
        <body>
        <table>
                <!-- Build Dynamic select statement - $main_select_clause
-->
                <!-- May have no predicate, may have 1, may have more than
one             -->
                <!-- Need to check 
                (string-length((normalize_space(criteria params)) > 0)  -->

                <!--    $main_select_clause = "records/record"
-->
                <!--    test length of each criteria            -->
                <!--    append predicate if appropriate []      -->

                <!--    <xsl:apply-template select="$main_select_clause"/>
-->

                <xsl:apply-templates 
                select="records/record[contains($letter_criteria,
@letter)][contains($color_criteria, @color)]"/>

        </table>
        </body>
        </html>
</xsl:template>

<xsl:template match="records/record">

        <tr>
                <td><xsl:value-of select="@num"/></td>
                <td><xsl:value-of select="@letter"/></td>
                <td><xsl:value-of select="@color"/></td>
        </tr>

</xsl:template>

</xsl:stylesheet>

****************************************************************



----


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread