Altova Mailing List Archives


Re: Regular expression functions (Was: Re: [xsl] comments on December F&O draft)

From: Jeni Tennison <jeni@---------------->
To:
Date: 1/14/2002 5:59:00 AM
Hi Chris,

>>   - open up normal templates so that they can match things other than
>>     nodes
>
> What is wrong with that? A template that matches text is pretty much
> the end of the line anyway.

Nothing's wrong with that. I think it's a good idea generally (as I
think I've argued here before).  It's not something that's currently
allowed, though.

One of the reasons that David and I have been talking about:

>>   - introduce specific regexp templates

instead is, I think, that it makes it easier for the processor to
identify when it has to keep track of the results of a regular
expression. Not that you *couldn't* do this with the kind of pattern
syntax that you were suggesting, it's just that I think it would be a
bit more difficult.

>> Unfortunately, assuming greedy, (a)(b) would produce:
>> 
>>   <x>a)(b</x>
>
> Yeh but it doesn't have to be greedy.
>
> <xsl:template match="\((.*?)\)(.*)">
>         <x><xsl:apply-templates select=".[1]" /></x>
>         <xsl:apply-templates select=".[2]" />
> </xsl:template>
> Or
> <xsl:template match="\((.*?)\)">
>         <x><xsl:apply-templates select=".[1]" /></x>
>         <xsl:apply-templates select="$'" />
> </xsl:template>

...OK, now try running those templates with your original string
"(a(b(c)d)e)". I think that you just get:

  <x>a(b(c</x>

Dimitre and David have kindly explained to me that regular expressions
cannot be used for nested structures such as these ones because a
language that permits nested structures are not regular languages, and
therefore cannot be described by regular expressions. (I think I got
that right - I'm sure I'll be corrected if I didn't ;)
  
[Just to point out that there's no syntax for non-greedy matches in
the XML Schema regular expression definition - I've posted to one of
the comments list about this (I think) but if you think it's a useful
thing to have, I'd recommend commenting to
www-xml-query-comments@xxxxxx as well.]

>> Or the other option is to have a special syntax to refer to a
>> regular expression, 
>
> You mean like text()['regexp']
> Which can't be confused with text()[normalize-space()]

This is equivalent to making there be a difference between:

  text()[2]
  text()[last()]

One of the features of functions is that when you call them, the call
is replaced by the result of the call. So if the context size is 2,
then text()[last()] is equivalent to text()[2].

You could say that literal strings got special handling in predicates
in the same way that literal numbers get special handling in
predicates. So when you have a literal string in a predicate it's
equivalent to a call on a test() function of some sort, with the
context item as the first argument and the string as the second
argument. But this would fundamentally change what
text()[normalize-space()] actually meant, which I think is quite a
large backwards-incompatible change.

I was meaning something like (desperately trying to find a suitable
ASCII character...):

  text()[%regexp%]

>> The way I (and I think David) was thinking, you'd use
>> current-match() or some other function to get information about the
>> subexpression matches when you were inside the template. So
>> perhaps:
>> 
>>   current-match()[x]
>> 
>> rather than .[x].
>
> Well if you like typing ;-)

Not particularly, but I do value consistency :) The other thing we'd
discussed was the implicit assignment of variables $1, $2 and so on -
if you don't like typing perhaps they're more your cup of tea.

I kinda decided I didn't like the idea because XSLT never implicitly
assigns values to variables anywhere else. I also thought that
current-match() fitted in better with current-group(), which does the
same kind of thing (adds something to the available context).

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.