Using current() within a pattern
Rujith de Silva <desilva <at> netbox.com>
2003-10-30 18:11:41 GMT
The XSLT spec, section 12.4, says:
It is an error to use the current function in a pattern.
I understand why the current() function is useful only within nested
expressions, and is hence usually not of use in a pattern. However,
if the pattern has doubly-nested square brackets, then the current()
function could be used to refer to the element two levels out.
When would that be useful? Quite often, actually. For example,
consider an XML file that looks like:
<employee name="Fred" title="CEO"/>
<employee name="Jane" title="CFO"/>
Then to match existing employees that have been laid off, the
following pattern could be used:
match="existing/employee[/company/layoff/employee[ <at> name=current()/ <at> name]]">
This finds the existing employees such that there's a layoff employee
element with a matching name. The idiom is fairly clear, and uses
current() in an intuitive way. current() within the [[doubly-nested
brackets]] refers, not to the element at the singly-nested level
[/company/layoff/employees], which would be unnecessary, but rather to
the next level up existing/employee, so it does the test just as
This is entirely consistent with the typical usage of current() within
templates. For example, the spec gives the following example:
<xsl:apply-templates select="//glossary/item[ <at> name=current()/ <at> ref]"/>
In this construct as well, current() refers, not the immediately
enclosing element //glossary/item, but to the next enclosing level,
which is the element that matched the template. In each case,
current() within a path predicate refers, not to the Path element to
which that predicate applies, but to the node at the next level.
Furthermore, the construct with current() within a pattern works fine
with Apache's Xalan processor. Xalan appears to ignore the
restriction regarding where current() may be used, and does the "right
thing" with the above construct.
Given the above, I suggest that the restriction against using
current() within a pattern be removed from the spec, or relaxed to
permit the above usage.
I've attached the following files:
- employees.xml (input)
- layoff.xml (output)
Rujith de Silva.