[thelist] XSLT search and replace Regex and escaping Special Characters...

Hassan Schroeder hassan at webtuitive.com
Sat Apr 23 17:42:05 CDT 2005


Mozer, Matt wrote:
> I am trying to do a search and replace on an element with a regex. The
> problem I am having is on the replace. I am looking for any http(s) strings
> and needing to create an href out of it.
/
> <xsl:template match="whatever">
>  <xsl:copy>
> <xsl:value-of select='replace(.,"(https?:\/\/[^\s]*)", "<a
> href=$1>$1</a>")'/>  
>  </xsl:copy>
> </xsl:template>
/
> I can't figure out how to escape the "<" & ">" characters... 

Maybe because you shouldn't? :-)   Here's what I use in what I'm
guessing is a similar situation, where, for example:

<addr>http:/example.com/foobar.html</addr>

<xsl:template match="//addr">
    <xsl:element name="a">
       <xsl:attribute name="href">
          <xsl:value-of select="."/>
       </xsl:attribute>
       <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

Obviously, if you're trying to match strings within a text element
of arbitrary content and length, it's a little more complex, but
the above shows how to turn your found value into a link...

HTH!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.




More information about the thelist mailing list