xsl-list
[Top] [All Lists]

Re: Using variables or parameters in tests

2005-03-02 09:03:00
Playing tag-team with David....

At 06:33 PM 3/1/2005, he wrote:
  datetest = (date >= 20050228)

variables hold values not expressions, so you while this

<xsl:value-of
select="sum(/statement/item[$datetest and $electricitytest]/amount)"/>

is legal; $datetest would be a constant holding whatever value it is
bound to, it wouldn't get a new value relative to each item node.

Note that if the test were not relative to the context node, everything would work fine.

That is, if instead of needing to test "date >= 20050228" (is the value of any date child element >= 20050228?), you needed to test "//date >= 20050228" (is the value of any date in the document >= 20050228?), then you could do:

datetest = (//date >= 20050228)

$datetest would be a Boolean with value "true" or "false", allowing you to perform the test anywhere the variable is in scope just by saying test="$datetest" or "expr[$datetest]".

Since the test is an absolute, global test, it makes sense to bind it to a variable at the top level so it has global scope.

I've used this technique on occasion and it works fine. The local version, scoped to a template, also works, but naturally gives you less bang for your buck (how many times do you have to do the same test in a given template)?

The essence of why this won't work with a relative expression inside a predicate is that the expression "date >= 20050228" is not actually the same test every time: when the context node is different, the test is different. This would be true in XSLT 2.0 too, which is why this limitation persists (although being able to define a function, as David shows, provides another way to approach the problem).

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>