?
Solved

Filter on multiple params in XSL

Posted on 2006-04-18
5
Medium Priority
?
863 Views
Last Modified: 2012-05-05
I'm trying to pass multiple params into an XSL stylesheet in order to filter the contents of the output based on the values of certain XML elements.  The default state of the stylesheet should not filter on these params.  The gist of what I've got so far:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:param name="filterBy">FLAGGED</xsl:param>
  <xsl:param name="filterValue">1</xsl:param>
  <xsl:param name="filterBy2">PREFERRED</xsl:param>
  <xsl:param name="filterValue2"></xsl:param>
   
     <xsl:template match="/">
       <xsl:for-each select="//APPLICANT[*[(name() = $filterBy and . = $filterValue)]]">
         
        ...(table built from <xsl:value-of select="FLAGGED">, etc. )...

       </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

This only displays rows where filterBy = filterValue, which is good.  What I need is for this to default to NOT filtering AND to be able to simultaneously filter by other parameters, too.  I'd rather not put the filtering in the XML transform function if I don't have to.  
0
Comment
Question by:etd_onlineguys
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16478521
Hi etd_onlineguys,

you can make the parameters empty by default, which is good practice
  <xsl:param name="filterBy"/>
  <xsl:param name="filterValue"/>
  <xsl:param name="filterBy2"/>
  <xsl:param name="filterValue2"/>

and make your test like this
    <xsl:for-each select="//APPLICANT[*[( (name() = $filterBy or string-length($filterBy) = 0) and ...)]]">

which is true if you don't pass a parameter

cheers

Geert
0
 
LVL 2

Author Comment

by:etd_onlineguys
ID: 16491257
Hi Gertone,
Thanks for the response.  The string-length() trick is a good one and I've got this working with one filter parameter.  I'm still unsure of how to get the additional filter parameters to work.   I tried using multiple predicates

<xsl:for-each select="//APPLICANT[*
        [(name() = $filterBy or string-length($filterBy) = 0) and . = $filtervalue]
        [(name() = $filterBy2 or string-length($filterBy2) = 0) and . = $filterValue2]
    ]">

but ended up with no results even though some rows met both filter criteria.  What am I missing?

David
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 16491425
two predicates chained is an implicit AND operation
I see four conditions that should all four be true
two of the conditions are . = $filtervalue and . = $filterValue2
this condition can only be met if $filtervalue = $filterValue2
That is likely not true... so you will get no results

do you want an OR between the two predicates?
then you need a union
<xsl:for-each select="//APPLICANT[
        * [(name() = $filterBy or string-length($filterBy) = 0) and . = $filtervalue]
       | *[(name() = $filterBy2 or string-length($filterBy2) = 0) and . = $filterValue2]
    ]">

cheers

Geert
0
 
LVL 2

Author Comment

by:etd_onlineguys
ID: 16491645
Gertone,
That was exactly what I needed!  Thanks!
David
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16491907
welcome David
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question