This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Using XSLT to search XML


Ema,

>- Users need to be able search on any one of about 4
>parameters (clubname, location, postcode, etc), any
>one of which can be null. However, at least one search
>parameter is required before a search is executed.
>
>- The search should be flexible enough to match any
>combination of parameters and should be able to
>exclude null parameters from the "select."

Given that you have separate variables containing the search string for
each of the parameters, then you could use the XPath:

  listing[(not($clubname) or contains(clubname, $clubname)) and
          (not($eventname) or contains(eventname, $eventname)) and
          (not($clubpostcode) or contains(clubpostcode, $clubpostcode)) and
          (not($location) or contains(location, $location))]

If $clubname is not specified then the first condition is always true (and
hence has no filtering effect); if it *is* specified, then the first
condition only returns true if the clubname for the listing contains the
specified $clubname.  So a listing will be filtered out if the clubname
does *not* contain the specified $clubname.  Each of the other conditions
works in the same way.

With the above XPath, all the listings will be returned if none of the
parameters are specified.  If you want to prevent that happening, then
place the xsl:apply-templates within an xsl:if that tests that at least one
variable is specified:

<xsl:if test="$clubname or $eventname or $clubpostcode or $location">
  <xsl:apply-templates select="content.item/content.body/listing[
    (not($clubname) or contains(clubname, $clubname)) and
    (not($eventname) or contains(eventname, $eventname)) and
    (not($clubpostcode) or contains(clubpostcode, $clubpostcode)) and
    (not($location) or contains(location, $location))]"
                       mode="dump" />
</xsl:if>

Just a quick comment or two about the rest of the code.  There's no need to
pass the parameters in to the 'dump' template: the parameters are defined
at the top level, so they're available for all the templates in the
stylesheet without being explicitly passed in.

The other thing is the XML: are you sure you need all those CDATA sections?
 The output that you'll get will be somewhat strange because of them.  For
example, if you get the value of:

      <clubcity><![CDATA[Elephant &amp; Castle]]></clubcity>

using xsl:value-of, then the HTML output will include the string:

  Elephant &amp;amp; Castle

which will be displayed in the browser as:

  Elephan &amp; Castle

I think it's more likely you just want to put:

      <clubcity>Elephant &amp; Castle</clubcity>

which will be outputted as:

  Elephant &amp; Castle

and hence displayed in the browser as:

  Elephant & Castle

I hope that this helps,

Jeni

Jeni Tennison
http://www.jenitennison.com/


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]