[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Using parameter as regex in matches() function

Subject: RE: Using parameter as regex in matches() function
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 29 May 2008 23:09:44 +0100
RE:  Using parameter as regex in matches() function
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:  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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.