xsl-list
[Top] [All Lists]

Re: regular expression in replace()

2005-10-18 05:05:03
i think i already solve my problem (i think :D) I used
a recursive function:

<xsl:function name="f:rep">
<xsl:param name="source" as="xs:string"/>
<xsl:param name="pattern" as="xs:string"/>
<xsl:param name="replace" as="xs:string"/>
<xsl:choose>
<xsl:when test="contains($source,$pattern)">
<xsl:value-of
select="substring-before($source,$pattern)"/>
<xsl:value-of select="$replace"/>
<xsl:value-of
select="f:rep(substring-after($source,$pattern),$pattern,$replace)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$source"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>

this replaces the replace() function. so when i call

<xsl:sequence select="f:rep('Lucie et Suz. Beauvais
Beauvais, Suzanne', 'Suz.', 'Suzanne')"/>

i can get:

Lucie et Suzanne Beauvais Beauvais, Suzanne

I hope this is correct :D

--- UlyLee <ulyleeka(_at_)yahoo(_dot_)com> wrote:

I have a string: "Lucie et Suz. Beauvais Suzanne"
and
i want to replace "Suz." with "Suzanne".

but when i use replace("Lucie et Suz. Beauvais
Suzanne","Suz.","Suzanne") it gives me "Lucie et
Suzanne Beauvais Suzannenne", i figured that this is
because "." is treated as a regular expression thats
why it replaced "Suza" with "Suzanne". I know i need
to escape the "." to  "\." but what if my
replace-pattern contains other regex characters like
"?" "*" "+"?

Michael Kay suggested that i first make my
replacement
string to regelar expression or create a replace
function that uses substring-before() and
contains().
How am i to go around this? I'm just starting out in
XSL and the new features of XSLT 2.0 sometime
confuses
me.

My first alternative was to use replace($sourceStr,
".", "\.") but it says "\." is an invalid
replacement string.


              
__________________________________ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/


--~------------------------------------------------------------------
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>
--~--





        
                
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.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>