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 | 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 August 24th, 2006, 08:08 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default if test=????

I have a field to search and a search string. I need all the records whose field starts with the search string. Something like.

For example given the following xml file I would like to be a able to do something like:
return all cd elements whose artist's name starts with "Bo"


<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>



Im not quite sure how to write the test for that

<xsl:for-each select="cd">
  <xsl:if test="???????? what goes here ??????">

Thanks

Reply With Quote
  #2 (permalink)  
Old August 24th, 2006, 08:27 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to use the starts-with function:
Code:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:param name="artistSearch" select="'Bo'"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>CD Collection</title>
      </head>
      <body>
        <table>
          <caption>
            Artists starting with#xa0;<xsl:value-of select="$artistSearch"/>
          </caption>
          <thead>
            <tr>
              <th>Title</th>
              <th>Artist</th>
              <th>Country</th>
              <th>Company</th>
              <th>Price</th>
              <th>Year</th>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="catalog/cd[starts-with(artist, $artistSearch)]" />
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="cd">
    <tr>
      <td>
        <xsl:value-of select="title"/>
      </td>
      <td>
        <xsl:value-of select="artist"/>
      </td>
      <td>
        <xsl:value-of select="country"/>
      </td>
      <td>
        <xsl:value-of select="company"/>
      </td>
      <td>
        <xsl:value-of select="price"/>
      </td>
      <td>
        <xsl:value-of select="year"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old August 24th, 2006, 08:34 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

almost what I need...
In "starts-with(artist, $artistSearch)" I need the field to be a parameter too
So someone could search by artist, price, title, etc.

I dont think I can just have it as a parameter:
starts-with($field, $artistSearch)... because it will just compare the two strings. I need to get the element whose name matches $field

Thanks

Reply With Quote
  #4 (permalink)  
Old August 24th, 2006, 09:41 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If they can only search by one field then:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="field" select="'title'" />
  <xsl:param name="search" select="'H'"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>CD Collection</title>
      </head>
      <body>
        <table>
          <caption>
            <xsl:value-of select="$field"/> starting with <xsl:value-of select="$search"/>
          </caption>
          <thead>
            <tr>
              <th>Title</th>
              <th>Artist</th>
              <th>Country</th>
              <th>Company</th>
              <th>Price</th>
              <th>Year</th>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="catalog/cd[starts-with(*[name() = $field], $search)]" />
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="cd">
    <tr>
      <td>
        <xsl:value-of select="title"/>
      </td>
      <td>
        <xsl:value-of select="artist"/>
      </td>
      <td>
        <xsl:value-of select="country"/>
      </td>
      <td>
        <xsl:value-of select="company"/>
      </td>
      <td>
        <xsl:value-of select="price"/>
      </td>
      <td>
        <xsl:value-of select="year"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old August 24th, 2006, 10:53 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

all cd elements whose artist's name starts with "Bo"

==>

cd[starts-with(artist, 'Bo')]


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old August 24th, 2006, 10:56 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can do starts-with(*[name()=$param], $searchstring)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #7 (permalink)  
Old August 24th, 2006, 01:51 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Awesome thanks!

One last thing I would like to make the search case insensitive.
I have tried to convert them both to upper case.. with no luck:

catalog/cd[starts-with(upper-case(*[name() = $searchfield]), upper-case(($searchtext))]

Thanks for all your help.

Reply With Quote
  #8 (permalink)  
Old August 24th, 2006, 03:35 PM
Authorized User
 
Join Date: Nov 2004
Location: , , .
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

You want the fieldname to not matter as well?
catalog/cd[starts-with(upper-case(*[upper-case(name()) = upper-case($searchfield])), upper-case(($searchtext))]

Also, you may need normalize-space().
Reply With Quote
  #9 (permalink)  
Old August 24th, 2006, 06:36 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No. I do not want the fieldname to not matter as well.
Just need the search strings case not to matter. So if someone searches "bo" they will get back "BOB".

It didnt look like you had changed the statement I had (besides for adding in the stuff to ignore fieldname case)

Here is what I have...
catalog/cd[starts-with(upper-case(*[name() = $searchfield]), upper-case(($searchtext))]

When I try to do the transformation, I get an error. If I remove the upper-case statements it works. So I'm guessing that I am using the upper-case statement incorrectly.

How would I change that statement so that it returns elements whose contents match the searchstring (case shouldnt matter).

Thanks

Reply With Quote
  #10 (permalink)  
Old August 25th, 2006, 02:54 AM
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The upper-case() function is in XPath 2.0 only. In 1.0, use

translate($x, 'abcdefg...', 'ABCDEFG...')

so long as the strings you are dealing with are confined to the English alphabet.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
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
test test Crystal Reports 0 August 21st, 2007 01:35 PM
Please help me test this, please larry HTML Code Clinic 0 June 11th, 2007 11:51 PM
test kristina Crystal Reports 0 June 14th, 2005 12:41 PM
test flyfish Access 0 January 21st, 2005 08:37 PM
test pettyqiang BOOK: ASP.NET Website Programming Problem-Design-Solution 1 June 10th, 2004 02:44 AM



All times are GMT -4. The time now is 01:52 PM.


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