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

Subject: Re: Regular expression functions (Was: Re: [xsl] comments on December F&O draft)
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 8 Jan 2002 18:03:41 +0000
David,

> <xsl:apply-regexp-templates-to-string select="text()"/>
>
> ...
>
> <xsl:regexp-template match="\sqrt{([^{}]*)}">
> <msqrt>
> <xsl:apply-regexp-templates-to-string select="$1"/>
> </msqrt>
> </xsl:regexp-template>
>
> <xsl:regexp-template match="\gamma">
> <mi>&#947;</mi>
> </xsl:regexp-template>

Ooh, tasty...

Presumably the match patterns would match the entire string? So to do
the whitespace replacement stuff that I mentioned, you'd have:

<xsl:regexp-template match="([^&#xA;&#x9;&#xD;]*)([&#xA;&#x9;&#xD;])(.*)">
  <xsl:value-of select="$1" />
  <xsl:value-of select="if ($2 = '&#xA;') then '\n'
                        else if ($2 = '&#x9;') then '\t'
                        else if ($2 = '&#xD;') then '\r'" />
  <xsl:apply-regexp-templates-to-string select="$3" />
</xsl:regexp-template>


The big weakness if this was the *only* method of doing matches is
that you can't use it with dynamic regular expressions - for example
match strings that contain the keyword $keyword as a word. Any ideas
around that one?


On a kind of related topic, I did wonder whether XSLT 2.0 would
support templates that matched things other than nodes (but it seems
not). It occurred to me that this could be useful if, for example, you
wanted all your numbers to be formatted in the same way throughout the
document - you could have:

<xsl:template match="value of type xs:decimal">
  <xsl:value-of select="format-number(., '#,##0.00')" />
</xsl:template>

[Note slight adjustment to the syntax for specifying datatypes, to
avoid confusion with element names.]

and then apply templates to whatever values wherever you found them
(even if they were found via substring or in sequences or whatever).


If there were string-regexp templates and not general value-matching
templates, then people might be tempted to use regular expressions to
identify data types in their match patterns instead.

Cheers,

Jeni

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


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


Current Thread