[xsl] RE: XPath expression to perform 'keyword' query

Subject: [xsl] RE: XPath expression to perform 'keyword' query
From: Richard Lewis <richard.lewis@xxxxxxxxx>
Date: Sat, 18 Oct 2003 13:19:05 +0100
OK,

Thanks for the suggestions. I'm making steady progess!

I've now got it so that the 'library.xml'(1) file is first processed by one 
stylesheet(2) and generates 'results.xml'(3) and then this file is processed 
by a second stylesheet(4) to dipsplay the search results.

I still have one problem.  I can't get it to search on more than one keyword. 
I think this is because I'm using the contains() function like this:

contains({element content}, $keywords)

Any ideas?

Cheers,
Richard.

FILE TYPES:
------------------------------------------------------------------------------------------------------------------
(1): library.xml is in this format
------------------------------------------------------------------------------------------------------------------

<library>
  <item id="...">
    <id>...</id>
    <media>...</media>
    <contents>...</contents>
    <title>...</title>
    <date_published>...</date_published>
    <publisher>...</publisher>
    <serial_number>...</serial_number>
    <date_aquired>...</date_aquired>
      <work>
        <title>...</title>
        <composer>...</composer>
        <date_composed>...</date_composed>
        <date_recorded>...</date_recorded>
        <period>...</period>
        <genre>...</genre>
        <duration>...</duration>
            <tracks>
                <track>
                    <number>...</number>
                    <title>...</title>
                    <duration>...</duration>
                </track>
                ....
            </tracks>
            <artists>
                <artist>...</artist>
                ....
            </artists>
         </work>
         ....
    </item>
    ....
</library>

------------------------------------------------------------------------------------------------------------------
(2): simple-search-get-results.xsl (with param keywords=....)
------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>

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

<xsl:output method="xml" />

<xsl:param name="keywords" />
<xsl:variable name="upperCase" select='ABCDEFGHIJKLMNOPQRSTUVWXYZ' />
<xsl:variable name="lowerCase" select='abcdefghijklmnopqrstuvwxyz' />
<xsl:variable name="transKeywords" select="translate($keywords, $upperCase,
  $lowerCase)" />

<xsl:template match="/">

<results>
  <xsl:for-each select="//item">
    <xsl:for-each select="descendant-or-self::*">
      <xsl:if test="contains(translate(.,$upperCase, $lowerCase), 
$transKeywords)">
        <item>
          <id><xsl:value-of select="ancestor-or-self::item/id" /></id>
          <title><xsl:value-of select="ancestor-or-self::item/title"/></title>
          <publisher><xsl:value-of select="ancestor-or-self::item/publisher"
/></publisher>
          <date_published><xsl:value-of 
select="ancestor-or-self::item/date_published" /></date_published>
        </item>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</results>

</xsl:template>

<xsl:template match="id" />
...
<!-- ignore all other elements -->

</xsl:stylesheet>

------------------------------------------------------------------------------------------------------------------
(3): results.xml is in this format
------------------------------------------------------------------------------------------------------------------

<results>
  <item>
    <id>...</id>
    <title>...</title>
    <publisher>...</publiser>
    <date_published>...</date_published>
  </item>
  ....
</results>

------------------------------------------------------------------------------------------------------------------
(4): simple-search-display-results.xsl
------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>

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

<xsl:output method="html" />

<xsl:template match="/">
  <xsl:for-each select="//item/id">
    <xsl:if test="position() = 1 or . != preceding::id[1]">
      <tr bgcolor="#C0C0C0">
        <td><a href="item-details.php?id={.}"><xsl:value-of select="."
/></a></td>
        <td><a href="item-details.php?id={.}"><xsl:value-of select="../title" 
/></a></td>
        <td>
          <xsl:value-of select="../publisher" />
          <xsl:text>, </xsl:text>
          <xsl:value-of select="../date_published" />
        </td>
      </tr>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template match="id" />
<xsl:template match="title" />
<xsl:template match="publisher" />
<xsl:template match="date_published" />

</xsl:stylesheet>
------------------------------------------------------------------------------------------------------------------


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread