xsl-list
[Top] [All Lists]

Re: xsl filter problem

2002-12-26 04:05:37
Yuval wrote:
My xml looks like this :

<customers>
      <customer>
              <city>a</city>
              <name>Jhon<name>
      </customer>
      <customer>
              <city>b</city>
              <name>Don<name>
      </customer>
      <customer>
              <city>c</city>
              <name>Ron<name>
      </customer>
</customers>

The data is to presented in a table and I want to filter it according to
the "city" - meaning I have 4 options :
1.a
2.b
3.c
4.all the cities = no filter

Im using a html select box to let the user choose which city to filter
and the first option is "all cities".for this parameter I give the value
*.

The xsl looks like this :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
      <xsl:output method="xml" encoding="windows-1255" />
      
      <xsl:param name="city"  />
              
      <xsl:template match="/">
              <customers>             
                      <xsl:for-each
select="customers/customer[city=$city]">
                              <customer>
                                      <xsl:for-each select="*">
                                              <xsl:element
name='{name()}'>
                                                      <xsl:value-of
select="." />
                                              </xsl:element>
                                      </xsl:for-each>
                              </customer>
                      </xsl:for-each>
              
              </customers>
      </xsl:template>
</xsl:stylesheet>

When choosing "all cities" - the result is an empty table instead of the
list of all cities with the customers names.
I tried to giva the param "city" a diect value of "*" or " '*' " and it
still didn't work.
How can I pass a value to this param which will act like : <xsl:for-each
select="customers/customer[city=*]"> ??

Don't forget to quote the *.

It looks to me like your conditions are as follows:

If $city = '*', you want all customer elements.
If $city != '*', you want all customer elements for which child::city = $city.

Rephrase this into a test for each customer element:

You want all customer elements for which:
  $city = '*' is true
  or
  child::city = $city

The answer, unless I've overlooked something, is:

customers/customer[$city='*' or city=$city]

Another option is to let $city be an empty string to indicate "all cities".
Then you'd use:

customers/customer[not($city) or city=$city]

Also note that instead of <xsl:element name="{name()}">...</xsl:element>
you can use <xsl:copy>...</xsl:copy>.

Mike

-- 
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/


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



<Prev in Thread] Current Thread [Next in Thread>