Re: [xsl] use variable in <xsl:if test=

Subject: Re: [xsl] use variable in <xsl:if test=
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 01 Mar 2002 18:55:41 -0500
Robert,

Could you be more specific by what you mean by "not work"?

A variable in a test expression is evaluated just as is any other expression. The test expression requires a Boolean. XPath has quite clear rules (albeit somewhat complex) to determine how different data object types such as node sets (the type you have set your variable to, below) are cast as Booleans. (In this case, a node-set tests as true if it is not empty, i.e. if it has one or more nodes in it.)

If it's not behaving as you expect, that's either because:

1 You aren't expecting the right thing
2 There is a bug in your processor

We need to nail down #1 before we can entertain #2 (though you might reassure us by telling us what processor you're using: what bugs may be found in the commoner ones, e.g. Saxon, MSXML, Xalan, are pretty well known on this list).

If you're expecting the wrong thing, it's likely because you don't understand how variables are set, or because you don't understand how expressions are evaluated as Booleans.

So what do you mean, "it doesn't work"? Are you getting an error?

In the code below, if you are setting the variable at the top level to be available globally, and you want it to contain any element throughout that contains an author element whose value is the string "C. J. Date", then try

<xsl:variable name="condition" select="//*[.//author='C. J. Date']"/>

But this is just a guess, since you haven't told us either what you want your variable to hold, where you are declaring it (this is important since XPath works relative to a context node), and what your source looks like (so we can judge whether the XPath is constructed correctly).

As for the XSLT itself, <xsl:if test="$condition">...</xsl:if> is legal and done all the time; so if it's not working you need to tell us how you know it's not working, and how you would know if it were. The problem is probably in your XPath.

(For example -- another wild guess -- are you aware the instruction <xsl:copy-of select="*"/> means "copy all the element *children* of the context node"?)

Regards,
Wendell

At 06:21 PM 3/1/2002, you wrote:
How in the world ;-) can i use a variable in <xsl:if test=..>

<xsl:variable name="condition" select="*[.//author='C. J. Date']"/>

  <xsl:template match="node()[name()=$entity]">
 <xsl:if test="boolean(string($condition))">
   <xsl:copy-of select="*"/>
 </xsl:if>
  </xsl:template>

This does not work neigther with
{$condition}
nor
anything else.


======================================================================
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
======================================================================


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



Current Thread