Re: more struggles with POWDER test materials

Hi Cameron,

Thanks for the snippet, which solves the problem as stated: however there was more to the problem than that: namely we need to regex-escape the string (not just deal with spaces). There may be a way to recurse on the string to achieve this: identify a segment before any of the regex reserved characters, concat() the escape, recurse until complete; but the problem there is that the escaped string still contains the reserved character. XSLT 1's translate() function can only cope with the translation of one character into another so that doesn't help us. 

I wouldn't be surprised if one of the XSLT 1 gurus could find a heavyweight multi-template way around this; however it is exactly the sort of problem that replace() was introduced to deal with in XSLT 2. Any suggestions gratefully received but not something I will have time to change at the moment - my assumption was that XSLT 2 was an approved recommendation and hence appropriate for the task.


Kevin Smith
 Vodafone R&D

----- Original Message -----
From: <>
To: Phil Archer <>
Cc: Harry Halpin <>; Dan Connolly <>; public-powderwg <>; <>
Sent: Fri May 15 08:47:27 2009
Subject: Re: more struggles with POWDER test materials

Phil Archer:
> We need the replace() function in XSLT 2 to convert our white space  
> separated lists into regular expressions. In simplified form, something 
> like
> <includehsosts></includehosts>
> becomes
> <includeregex>[regex_blurb] (| [more_regex_blurb]  
> </includeregex>
> And (our XML expert reliably informs me) you can't do that in XSLT 1.

You can, it just takes a bit of extra work:

zot:/tmp $ cat b.xsl; echo
<xsl:stylesheet xmlns:xsl=''
  <xsl:template match='includehosts'>
      <xsl:text>[regex_blurb] (</xsl:text>
      <xsl:call-template name='replace-space-with-bar'>
        <xsl:with-param name='s' select='.'/>
      <xsl:text>) [regex_blurb]</xsl:text>

  <xsl:template name='replace-space-with-bar'>
    <xsl:param name='s'/>
      <xsl:when test='contains($s, " ")'>
        <xsl:value-of select='substring-before($s, " ")'/>
        <xsl:call-template name='replace-space-with-bar'>
          <xsl:with-param name='s' select='substring-after($s, " ")'/>
      <xsl:otherwise><xsl:value-of select='$s'/></xsl:otherwise>

zot:/tmp $ cat b.xml; echo

zot:/tmp $ xsltproc b.xsl b.xml
<?xml version="1.0"?>
<includeregex>[regex_blurb] (| [regex_blurb]</includeregex>

Cameron McCormack ≝

Received on Friday, 15 May 2009 08:04:21 UTC