RE: [xsl] Using parameter as regex in matches() function

Subject: RE: [xsl] Using parameter as regex in matches() function
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 29 May 2008 23:09:44 +0100
You want

matches(., concat('^', $criteria_name, '$'))

Incidentally, don't do this:

<xsl:with-param name="criteria_name">
  <xsl:value-of select="."/>
</xsl:with-param>

when you mean this:

<xsl:with-param name="criteria_name" select="."/>

It's a very common blunder, and it gives XSLT an undeserved reputation for
verbosity and slowness. You're building an XML document containing a tree of
nodes, when all you want is to evaluate a string.

Michael Kay
http://www.saxonica.com/ 


> -----Original Message-----
> From: Dave.McGovern@xxxxxxxxxxx [mailto:Dave.McGovern@xxxxxxxxxxx] 
> Sent: 29 May 2008 22:46
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Using parameter as regex in matches() function
> 
> I am using XSLT 2.0 using the Altova XMLSpy 2008 built-in engine.
> 
> I am trying to use an xsl:param parameter name as my regex within a
> matches() function.
> 
> I am passing the parameter in via a call to a named template as shown
> here:
> 
> ...
> <xsl:call-template name="assign_id">
> <xsl:with-param name="criteria_name"><xsl:value-of
> select="."/></xsl:with-param>
> </xsl:call-template>
> ...
> 
> Here is the called template:
> ...
> <xsl:template name="assign_id">
> <xsl:param name="criteria_name">NULL</xsl:param>
> <xsl:attribute name="id"><xsl:value-of
> select="document('dictionary/subselectionCriteria.xml')//dt[ma
> tches(.,$c
> riteria_name)]/ancestor::dlentry/@id"/></xsl:attribute>
> <xsl:value-of select="."/>
> </xsl:template>
> ...
> 
> With the problematic part being:
> //dt[matches(.,$criteria_name)]
> 
> The problem is that I want to set boundaries for my regex 
> using the ^ and $ meta-characters.  As I am using it above, 
> the matches() function is behaving more like contains().
> 
> For example, if $criteria-name is set to "Reporting Group" on 
> a particular iteration, I am getting matches on "Reporting 
> Group", "From Reporting Group", "To Reporting Group", 
> "Reporting Group Type", etc.
> 
> I want to restrict this to only match on the exact "Reporting Group"
> string.
> 
> I have tried various quoting and backslash-escaping constructs to no
> avail:
> //dt[matches(.,'^$criteria_name$')]
> //dt[matches(.,'^\$criteria_name$')]
> //dt[matches(.,'^{$criteria_name}$')]
> 
> I think this may be because the same metacharacter '$' is 
> used for both the param name and to set the end boundary of 
> the regex, but I'm not sure why escaping it wouldn't work if 
> this is the case?
> 
> Any ideas?
> Thanks in advance,
> Dave

Current Thread