Filter on multiple params in XSL

Posted on 2006-04-18
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="" >
  <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. )...


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.  
Question by:etd_onlineguys
    LVL 60

    Expert Comment

    by:Geert Bormans
    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


    LVL 2

    Author Comment

    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?

    LVL 60

    Accepted Solution

    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]


    LVL 2

    Author Comment

    That was exactly what I needed!  Thanks!
    LVL 60

    Expert Comment

    by:Geert Bormans
    welcome David

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now