Re: [xsl] Using variables or parameters in tests

Subject: Re: [xsl] Using variables or parameters in tests
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 02 Mar 2005 11:03:00 -0500
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

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).


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 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