RE: Using current() within a pattern

The XSLT 2.0 working draft has removed the restriction on using current()
within a pattern. The current() function, when used within a pattern, is
defined to return the node that is being tested against the pattern.


Michael Kay

> -----Original Message-----
> From: Rujith de Silva [] 
> Sent: 30 October 2003 18:16
> To:
> Subject: Using current() within a pattern
> 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:
>    <existing>
>      <employee name="Fred" title="CEO"/>
>      <employee name="Jane" title="CFO"/>
>      ...
>    </existing>
>    <layoff>
>      <employee name="Fred"/>
>      ...
>    </layoff>
> Then to match existing employees that have been laid off, the 
> following pattern could be used:
>    <xsl:template
> match="existing/employee[/company/layoff/employee[@name=curren
> t()/@name]]">
>      ...
>    </xsl:template>
> 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 desired.
> 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[@name=current()/@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)
> - employees.xsl
> - layoff.xml    (output)
> Thanks,
> Rujith de Silva.

Received on Friday, 31 October 2003 13:44:19 UTC