Search XML Node

Discussion in 'XML' started by thomas, May 3, 2005.

  1. thomas

    thomas Guest

    I'm building a guitar website which uses XML and XSLT.
    http://www.madtim67.com/guitar/index.html You can search either by artist or
    song. At the moment my XSL page only returns a result if the exact string is
    entered i.e I have to enter 'baker street' or 'gerry rafferty' in order to
    get a result. I would appriciate it if anyone could tell me how to modify
    the code below so that if I enter 'baker' or 'gerry' I would get a result.

    Heres my XSL page

    <?xml version="1.0"?>

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

    <xsl:param name="text1" />

    <xsl:template match="/">

    <html>

    <head>
    <link rel="stylesheet" type="text/css" href="mystyle.css" />
    <title>Results</title>
    </head>

    <body>

    <table class="three" align="center">

    <col width="35%" />
    <col width="35%" />
    <col width="15%" />
    <col width="15%" />

    <tr>

    <th class="head">Artist</th>
    <th class="head">Song</th>
    <th class="head">Chord</th>
    <th class="head">Midi</th>

    </tr>

    <xsl:for-each select="cat/links">
    <xsl:if test="./artist = $text1">

    <tr>
    <td><xsl:apply-templates select="./artist" /></td>
    <td><xsl:apply-templates select="./song" /></td>
    <xsl:variable name="link1"><xsl:apply-templates select="./chord"
    /></xsl:variable>
    <td><a href="{$link1}" target="_blank">view</a></td>
    <xsl:variable name="link2"><xsl:apply-templates select="./midi"
    /></xsl:variable>
    <td><a href="{$link2}" target="_blank">play</a></td>
    </tr>

    </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="cat/links">
    <xsl:if test="./song = $text1">

    <tr>
    <td><xsl:apply-templates select="./artist" /></td>
    <td><xsl:apply-templates select="./song" /></td>
    <xsl:variable name="link1"><xsl:apply-templates select="./chord"
    /></xsl:variable>
    <td><a href="{$link1}" target="_blank">view</a></td>
    <xsl:variable name="link2"><xsl:apply-templates select="./midi"
    /></xsl:variable>
    <td><a href="{$link2}" target="_blank">play</a></td>
    </tr>

    </xsl:if>
    </xsl:for-each>

    </table>

    <div align="center">
    <a href="javascript:history.go(-1)">Click here to return to search page</a>
    </div>

    </body>

    </html>

    </xsl:template>

    </xsl:stylesheet>



    Heres my XML file

    <cat>
    <links>
    <artist>gerry rafferty</artist>
    <song>baker street</song>
    <chord>media/gerry_rafferty_-_baker_street.txt</chord>
    <midi>media/gerry_rafferty_-_baker_street.mid</midi>
    </links>
    <links>
    <artist>men at work</artist>
    <song>down under</song>
    <chord>media/men_at_work_-_down_under.txt</chord>
    <midi>media/men_at_work_-_down_under.mid</midi>
    </links>
    <links>
    <artist>squeeze</artist>
    <song>up the junction</song>
    <chord>media/squeeze_-_up_the_junction.txt</chord>
    <midi>media/squeeze_-_up_the_junction.mid</midi>
    </links>
    <links>
    <artist>steve harley</artist>
    <song>make me smile</song>
    <chord>media/steve_harley_-_make_me_smile.txt</chord>
    <midi>media/steve_harley_-_make_me_smile.mid</midi>
    </links>
    </cat>
    thomas, May 3, 2005
    #1
    1. Advertising

  2. You could change
    <xsl:if test="./artist = $text1">

    to

    <xsl:if test="contains(artist ,$text1)">

    Note you never need to start an XPath with ./ also you don't really need
    an xsl:if here

    <xsl:for-each select="cat/links">
    <xsl:if test="./artist = $text1">

    can be written


    <xsl:for-each select="cat/links[artist = $text1]">

    which is often preferable as it means for example that position() just
    numbers the nodes that you actually want to output, useful for
    numbering, or making alternating couloured rows in tables etc.


    David
    David Carlisle, May 3, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,442
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,418
    Dimitre Novatchev
    Aug 24, 2006
  3. Martin Honnen

    Re: Reading XML Node-by-Node

    Martin Honnen, Nov 12, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    385
    Martin Honnen
    Nov 12, 2008
  4. Abby Lee
    Replies:
    5
    Views:
    393
    Abby Lee
    Aug 2, 2004
  5. Geagleeye

    how to creating new node inside a xml node

    Geagleeye, Aug 2, 2006, in forum: ASP General
    Replies:
    2
    Views:
    197
    Geagleeye
    Aug 3, 2006
Loading...

Share This Page