Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 3rd, 2008, 12:38 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>



Reply With Quote
  #2 (permalink)  
Old March 3rd, 2008, 12:58 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
Reply With Quote
  #3 (permalink)  
Old March 3rd, 2008, 01:04 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #4 (permalink)  
Old March 3rd, 2008, 01:06 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old March 3rd, 2008, 05:49 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #6 (permalink)  
Old March 4th, 2008, 12:14 PM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search a binary file of million records DineshGirij008 C# 18 July 17th, 2008 09:38 AM
Including some records and excluding others RussLewis XSLT 2 March 3rd, 2008 12:36 PM
VB SQL search similar records Derek_05 Access VBA 1 March 24th, 2006 08:19 PM
Excluding Rows fs22 SQL Language 2 August 17th, 2005 12:58 PM
excluding archives of a folder thomaz C# 4 March 8th, 2004 09:09 AM



All times are GMT -4. The time now is 05:51 AM.


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