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 & Castle]]></clubcity> using xsl:value-of, then the HTML output will include the string: Elephant &amp; Castle which will be displayed in the browser as: Elephan & Castle I think it's more likely you just want to put: <clubcity>Elephant & Castle</clubcity> which will be outputted as: Elephant & 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
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format