Re: [xsl] XSL search

Subject: Re: [xsl] XSL search
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 10 Jan 2008 16:42:30 -0500
Russ,

At 04:07 PM 1/10/2008, you wrote:
----------------
Here is where it gets hairy! (and I lose mine)
----------------

<xsl:for-each select="/log/logentry[$searchfield and contains(., $searchtext)]">

If $searchfield is a non-empty string, it will always coerce to a Boolean true(), so saying


logentry[$searchfield and contains(., $searchtext)]

is as much as to say

logentry[true() and contains(., $searchtext)]

... i.e. the first test is nugatory, which is probably not what you want.

You may want to be testing for a logentry that contains a child element named $searchfield, which contains the $searchtext. This (in XSLT 1.0) is

logentry[*[name() = $searchfield][contains(., $searchtext)]]

Similarly --

---------------- XSL snippet ----------------

<xsl:template match="log">
  <xsl:for-each select="logentry">
    <xsl:sort select="node()[$searchfield]" order="ascending"/>

If $searchfield is a non-empty string, saying "node()[$searchfield]" is the same as saying "node()", for the same reason as just given.


If you mean to sort by the values of the (first) element child named $searchfield, that would again be

*[name()=$searchfield]

etc.

I haven't looked over your code thoroughly, so there may be similar problems elsewhere. The pattern seems to be that you are writing XPath expressions that are syntactically correct, and which are therefore not giving you runtime errors, but which are also not doing what you expect them to.

The solution would seem to be a more careful and systematic approach to XPath than you are taking. I think you have hit the limit of how much XPath you can get right by guessing. This is not surprising: you are not attempting beginner-level XSLT.

Fortunately, there are treatments of XPath online, and also formal courses you can take (such as those offered at my company :-), which will remedy this. XPath isn't hard, but past the bare basics, it's not really very guessable either. Really, everything will make much more sense once you have it in your kit.

I hope this helps,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread