p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Excluding certain records from a search (http://p2p.wrox.com/showthread.php?t=66359)

RussLewis March 3rd, 2008 12:38 PM

Excluding certain records from a search
 
I'm trying to learn how/do two things here:
This works:
1) If the user searches for "entry" ($searchtext = "entry") the output should also include the fourth and fifth record because Field1 contains "all", "All", or "ALL" even though the fourth record doesn't contain any case of "entry". Also the fifth record should only be output once even though it contains any case of "all" in Field1 and also any case of "entry" in Field2. I've managed to accomplish this using this statement:
 <xsl:for-each select="record[*[name() = $searchfield][contains(., $ProperText) or contains(., $UCASETEXT) or contains(., $lcasetext) or ../Field1[contains(., 'ALL') or contains(., 'all') or contains(., 'All')]]]">

I can't make this work though:
 2) But I want the output of this same search to ALSO exclude the second and sixth record because Field1 contains "info", "Info" or "INFO" even though Field2 contains "Entry", "entry", or "ENTRY". The info records won't be viewed using this stylesheet. The records where Field1 contains any case of "info" will only show up when you use a different stylesheet much like this one in which case it will be treated just like this stylesheet treats records where Field1 is "all".
XML Data
<?xml version="1.0"?>
<data>
  <record>
    <Field1>Data1-1</Field1>
    <Field2>Entry1-2</Field2>
    <dtmField>2008-01-28T01:01:00Z</dtmField>
  </record>
  <record>
    <Field1>Info</Field1>
    <Field2>This entry is not readable</Field2>
    <dtmField>2008-01-28T02:02:00Z</dtmField>
  </record>
  <record>
    <Field1>Data3-1</Field1>
    <Field2>Entry3-2</Field2>
    <dtmField>2008-01-28T03:03:00Z</dtmField>
  </record>
  <record>
    <Field1>all</Field1>
    <Field2>This record is readable by everyone</Field2>
    <dtmField>2008-01-28T04:04:00Z</dtmField>
  </record>
  <record>
    <Field1>All</Field1>
    <Field2>This entry is also readable by everyone</Field2>
    <dtmField>2008-01-28T05:05:00Z</dtmField>
  </record>
  <record>
    <Field1>info</Field1>
    <Field2>This entry is also not readable</Field2>
    <dtmField>2008-01-28T06:06:00Z</dtmField>
  </record>
</data>

  <record>
    <Field1>Data1-1</Field1>
    <Field2>Entry1-2</Field2>
    <dtmField>2008-01-28T01:01:00Z</dtmField>
  </record>
Record 2 is excluded because Field1 = "Info"
  <record>
    <Field1>Data3-1</Field1>
    <Field2>Entry3-2</Field2>
    <dtmField>2008-01-28T03:03:00Z</dtmField>
  </record>
  <record>
    <Field1>all</Field1>
    <Field2>This entry is readable by everyone</Field2>
    <dtmField>2008-01-28T04:04:00Z</dtmField>
  </record>
Record 5 below is included only once even though Field2 contains "entry" AND Field1 contains "all"
  <record>
    <Field1>All</Field1>
    <Field2>This entry is also readable by everyone</Field2>
    <dtmField>2008-01-28T05:05:00Z</dtmField>
  </record>
Record 6 is excluded because "Field1" = info even though Field2 contains "entry"
XSL Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html"/>
  <xsl:param name="searchfield" select="Field2"/>
  <xsl:param name="searchtext" select="entry"/>
  <xsl:param name="sortbyfield" select="Field1"/>
  <xsl:param name="sortorder" select="Descending"/>
  <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  <xsl:variable name="lcasetext" select="translate($searchtext, $uppercase, $lowercase)"/>
  <xsl:variable name="UCASETEXT" select="translate($searchtext, $lowercase, $uppercase)"/>
  <xsl:variable name="ProperText" select="concat(translate(substring($searchtext,1,1 ), $lowercase, $uppercase),translate(substring($searchtext,2), $uppercase, $lowercase))"/>
  <xsl:template match="data">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title> Search Results </title>
      </head>
      <body>
        <p class="title"> Search Results </p>
        <table>
          <tr>
            <th> Field1 </th>
            <th> Field2 </th>
            <th> Date &amp; Time</th>
          </tr>
          <xsl:for-each select="record[*[name() = $searchfield][contains(., $ProperText) or contains(., $UCASETEXT) or contains(., $lcasetext) or ../Field1[contains(., 'ALL') or contains(., 'all') or contains(., 'All')]]]">
          <xsl:sort select="*[name()=$sortbyfield]" order="{$sortorder}"/>
            <tr>
              <td>
                 <xsl:value-of select="Field1"/>
              </td>
              <td>
                  <xsl:value-of select="Field2"/>
              </td>
              <td>
                  <xsl:value-of select="dtmField"/>
              </td>
             </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>




samjudson March 3rd, 2008 12:58 PM

Your problem does not appear to be the logic of your XPath condition at all, but in the variables you have declared at the start. Each needs to be wrapped in single quotes, as they are strings, not XML elements.

  <xsl:param name="searchfield" select="'Field2'"/>
  <xsl:param name="searchtext" select="'entry'"/>
  <xsl:param name="sortbyfield" select="'Field1'"/>
  <xsl:param name="sortorder" select="'descending'"/>


/- Sam Judson : Wrox Technical Editor -/

samjudson March 3rd, 2008 01:04 PM

Als, you do realise that the phrase 'This entry is not readable' contains the word 'entry'.

/- Sam Judson : Wrox Technical Editor -/

mhkay March 3rd, 2008 01:06 PM

I'm a bit confused as to your distinction between contains() (meaning "has as a substring") and equals - but perhaps that's a separate problem.

The basic logic seems to be

record[not(Field1 = 'info'])][Field1 = 'all' or *[name()=$searchfield and contains(., $searchtext)]

except that you have to expand the conditions to match text in either case - but you already know how to do that.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

RussLewis March 3rd, 2008 05:49 PM

Sam,
Sorry about that in the parameters but in the actual stylesheet the values for the parameters come from an ASP server-side app that merely feeds them to this stylesheet from another stylesheet masquerading as a user input form. So truthfully the parameters look like this:
  <xsl:param name="searchfield" />
  <xsl:param name="searchtext" />
  <xsl:param name="sortbyfield" />
  <xsl:param name="sortorder" />
I just filled them in (albiet incorrectly) for this post. (Sorry about that)
but your example of:
  <xsl:param name="searchfield" select="'Field2'"/>
  <xsl:param name="searchtext" select="'entry'"/>
  <xsl:param name="sortbyfield" select="'Field1'"/>
  <xsl:param name="sortorder" select="'descending'"/>
should at least make this work.
Also the part about "This entry is not readable" is one of the main things I'm trying to learn to do. While the fact that Field2 contains "entry" should qualify it for output, the fact that Field1 contains "info" should override that qualification and disqualify this record for output because no matter what the search finds in any field, if Field1 contains any standard case variation of "info" the record is NOT to be output to the table.

Michael,
I'm not sure if this changes you response but I'll give your response a try and see.

Thanks,
Russ Lewis


RussLewis March 4th, 2008 12:14 PM

Michael's suggestion worked after I removed the extra bracket after 'info'. However David Carlisle also gave me a suggestion on the Mulberry Tech List that exposed a limitation of your suggestion that I had never considered. While Field1 should only contain "Data...", or "Info", or "All" I have noticed an occasion where some users use this field incorrectly and therefore it may contain (even though it shouldn't) both "data" AND ("all" or "info"). My mistake was not considering the "idiot factor". This said, Mr. Carlisle's suggestion accounts for this (knowingly or unknowingly) and it also shows how I can use translate() against "info" and "all" instead of using multiple not()s. I show both implementations below for the sake of passing useful information in this forum. Thanks for everybody's help on this.

Michael Kay's suggestion:
record[not(Field1 = 'info')][Field1 = 'all' or *[name()=$searchfield and contains(., $searchtext)]

This suggestion works and answers my question as I asked it but if Field1 is entered incorrectly and contains both 'All' and 'Data3-1'(even though it shouldn't since 'Data3-1' and 'All' are redundant) this filter statement will not output these records. It also outputs records where Field1 is entered incorrectly and contains both 'info' and 'data' (even though it shouldn't either since this means that the user doesn't understand the use of this field). Below is the xsl implementation of this statement I used showing also multiple cases of 'info' and 'all'.
Like I said this should have worked if users always used Field1 correctly(I should have known better).

<xsl:for-each select="record[not(Field1 = 'info')][not(Field1 = 'Info')][not(Field1 = 'INFO')][Field1 = 'all' or Field1 = 'All' or Field1 = 'ALL' or *[name()=$searchfield and contains(., $searchtext)]]">

-----------------------------

David Carlisle's sugggestion:
record[not(contains(translate(Field1,$lowercase,$uppercas e),'INFO'))][contains(translate(Field1,$lowercase,$uppercase),' ALL') or *[name() = $searchfield][contains(translate(.,$lowercase,$uppercase),$UCASE TEXT)]]

Since Mr. Carlisle's suggestion uses the contains() function it outputs records even if Field1 is used incorrectly (which has happened) and contains both 'data' AND 'all'. It also Excludes records where Field1 may contain both 'data' and 'info'. His suggestion also uses my reusable translate() function variables $lowercase and $uppercase the way I should have thereby allowing me to get rid of $Propertext and $lcasetext which are just specific applications of the translate() function. Below is the xsl implementation that I used as a result:

<xsl:for-each select="record[not(contains(translate(Field1,$lowercase,$uppercas e),'INFO'))][contains(translate(Field1,$lowercase,$uppercase),' ALL') or *[name() = $searchfield][contains(translate(.,$lowercase,$uppercase),$UCASE TEXT)]]">

Thanks again,
Russ Lewis



All times are GMT -4. The time now is 03:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.