using wildcards in a select

H

Heltende

I'm trying to develop a stylesheet that takes a parameter that is used
to filter the results. It works great *except* for the fact that I
want one of my options to be a "See All", removing any filters.

I was hoping that I could do this by setting a parameter to default to
* and then removing the parameter when the user tries to see all. It
doesn't work though.

Here's what I have:


<xsl:param name="year_filter" select="*"/>
<xsl:template match="data">
....
<xsl:for-each select="holiday_list/row[year=$year_filter]">
<xsl:variable name="description" select="description"/>
....
</tr>
</xsl:for-each>

The following works:

<xsl:for-each select="holiday_list/row[year=*]">
<xsl:variable name="description" select="description"/>
....
</tr>
</xsl:for-each>

but I can't figure out how to get year_filter to equal '*'. The
problem with the first example is clear when I display the value of
$year_filter. It shows a concatenated list of all of my data.

I'm an xslt beginner, so if this is a stupid question, I beg your
forgiveness and patience!

TIA, Charles
 
M

Martin Honnen

Heltende said:
I'm trying to develop a stylesheet that takes a parameter that is used
to filter the results. It works great *except* for the fact that I
want one of my options to be a "See All", removing any filters.

I was hoping that I could do this by setting a parameter to default to
* and then removing the parameter when the user tries to see all. It
doesn't work though.

Here's what I have:


<xsl:param name="year_filter" select="*"/>
<xsl:template match="data">
...
<xsl:for-each select="holiday_list/row[year=$year_filter]">
<xsl:variable name="description" select="description"/>
...
</tr>
</xsl:for-each>

The following works:

<xsl:for-each select="holiday_list/row[year=*]">
<xsl:variable name="description" select="description"/>
...
</tr>
</xsl:for-each>

but I can't figure out how to get year_filter to equal '*'. The
problem with the first example is clear when I display the value of
$year_filter. It shows a concatenated list of all of my data.

You might want to set the parameter to an empty string (or the number 0)
to indicate you don't want to filter and then code e.g.
<xsl:template match="data">
<xsl:choose>
<xsl:when test="$year-filter">
<xsl:for-each select="holiday_list/row[year=$year_filter]">
...
</xsl:for-each>
</xsl:when>
<xsl:eek:therwise>
<xsl:for-each select="holiday_list/row">
...
</xsl:for-each>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
If you have a lot of code inside of the for-each then write a named
template and call that in the for-each to avoid code duplication.
 
H

Heltende

You might want to set the parameter to an empty string (or the number 0)
to indicate you don't want to filter and then code e.g.
<xsl:template match="data">
<xsl:choose>
<xsl:when test="$year-filter">
<xsl:for-each select="holiday_list/row[year=$year_filter]">
...
</xsl:for-each>
</xsl:when>
<xsl:eek:therwise>
<xsl:for-each select="holiday_list/row">
...
</xsl:for-each>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
If you have a lot of code inside of the for-each then write a named
template and call that in the for-each to avoid code duplication.



Thank you for the rapid response! That code does indeed work. It
doesn't necessarily "feel" like the most elegant method, but given
that the "*" seems to have a mind of it's own, depending on context, I
guess it's the method I'm stuck with! Now to explore the exciting
world of including named templates!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top