Quite an elegant solution Christopher, and for what I need more than fast
enough.

Thanks!

-----Original Message-----
From: Christopher Painter-Wakefield [mailto:[EMAIL PROTECTED] 
Sent: Montag, 4. August 2003 19:16
To: [EMAIL PROTECTED]
Subject: Re: regex transformer






an XSLT stylesheet isn't that difficult, but it might be slow doing
substring matches on every node.  A transformer might be faster, and easy
to write, but you will still be searching every node, and you have the
extra step of compiling, etc.  If you want to try it, I think this XSLT
will work (you will have to provide the two parameters through the sitemap,
I think).  This will replace a string within a block of text or within an
attribute value, but won't modify element or attribute names, unlike a pure
regex substitution.


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="
1.0">

<xsl:param name="search-string"/>
<xsl:param name="new-string"/>

<!-- basic copy template -->
<xsl:template match="@*|node()" priority="-1">
  <xsl:copy>
     <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{name()}">
     <xsl:call-template name="substring-replace">
       <xsl:with-param name="input" select="."/>
     </xsl:call-template>
  </xsl:attribute>
</xsl:template>

<xsl:template match="text()">
  <xsl:call-template name="substring-replace">
     <xsl:with-param name="input" select="."/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="substring-replace">
  <xsl:param name="input"/>
  <xsl:choose>
     <!-- empty search string produces infinite loop -->
     <xsl:when test="$search-string=''">
       <xsl:value-of select="$input"/>
     </xsl:when>
     <xsl:when test="contains($input, $search-string)">
       <xsl:value-of select="substring-before($input, $search-string)"/>
       <xsl:value-of select="$new-string"/>
       <xsl:call-template name="substring-replace">
          <xsl:with-param name="input" select="substring-after($input,
$search-string)"/>
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$input"/>
     </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>




|---------+---------------------------->
|         |           Jorg Heymans     |
|         |           <[EMAIL PROTECTED]|
|         |           nap.com>         |
|         |                            |
|         |           08/04/2003 11:53 |
|         |           AM               |
|         |           Please respond to|
|         |           users            |
|         |                            |
|---------+---------------------------->
 
>---------------------------------------------------------------------------
-----------------------------------|
  |
|
  |       To:       "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
|
  |       cc:
|
  |       Subject:  regex transformer
|
 
>---------------------------------------------------------------------------
-----------------------------------|




Hi all,





I would need some sort of search and replace functionality for my XML
templates, resorting to XSLT for this seems beyond my reach.


Basically if a template contains





<test>


<url value=http://mycom.com/?param=myparam/>


<title value="myparam"/>


</test>





Then I would like to replace all occurrences of myparam with a value of my
own. How would this fit into cocoon? Should I be looking at creating my own
transformer eg:


<map:transform type="regex">


<map:parameter regex="s/myparam/myvalue/g">


or


<map:parameter op="replace" what="myparam" with="myvalue"/>





</map:transform>





Does this seem all too crazy? Is there a better way of doing this?





Rgds


Jorg








---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to