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>example.com example.org</includehosts>
>
> becomes
>
> <includeregex>[regex_blurb] (example.com|example.org) [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='http://www.w3.org/1999/XSL/Transform'
                version='1.0'>
  <xsl:template match='includehosts'>
    <includeregex>
      <xsl:text>[regex_blurb] (</xsl:text>
      <xsl:call-template name='replace-space-with-bar'>
        <xsl:with-param name='s' select='.'/>
      </xsl:call-template>
      <xsl:text>) [regex_blurb]</xsl:text>
    </includeregex>
  </xsl:template>

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

zot:/tmp $ cat b.xml; echo
<includehosts>example.com example.org</includehosts>

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

-- 
Cameron McCormack ≝ http://mcc.id.au/

Received on Friday, 15 May 2009 06:48:29 UTC