xsl-list
[Top] [All Lists]

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

2008-05-29 15:10:17
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(_dot_)McGovern(_at_)sungard(_dot_)com 
[mailto:Dave(_dot_)McGovern(_at_)sungard(_dot_)com] 
Sent: 29 May 2008 22:46
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>