Re: Using variables or parameters in tests
At 06:33 PM 3/1/2005, he wrote:
datetest = (date >= 20050228)
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).
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format