[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Using XSLT to search XML

Subject: Re: Using XSLT to search XML
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 26 Oct 2000 11:55:32 +0100
dumping xml using xsl
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.