Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT 2.0 replace < > special characters

Reply
Thread Tools

XSLT 2.0 replace < > special characters

 
 
mattmozer@gmail.com
Guest
Posts: n/a
 
      04-25-2005
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.
This code below works for replace any http(s) with say a text result
such as "fred" or "bob" - for example. You can see what I am
trying to do with it though with the a href. I am using XML Spy on a
windows platform.

<xsl:template match="whatever">
<xsl:copy>
<xsl:value-of select='replace(.,"(https?:\/\/[^\s]*)", "<a
href=$1>$1</a>")'/>
</xsl:copy>
</xsl:template>

The element in this case "whatever" could have multiple http
references and are contained in a text element of arbitrary content and
length. So, with that being said, the below example is not what I'm
looking for as someone has already pointed out. I have tried something
similar to no avail.

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

I need to be able to escape the "<" & ">" characters or change my
approach entirely... I think this would prove to be a valuable tool if
it were to work.

-Matt

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-26-2005


http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
> This code below works for replace any http(s) with say a text result
> such as "fred" or "bob" - for example. You can see what I am
> trying to do with it though with the a href. I am using XML Spy on a
> windows platform.
>
> <xsl:template match="whatever">
> <xsl:copy>
> <xsl:value-of select='replace(.,"(https?:\/\/[^\s]*)", "<a
> href=$1>$1</a>")'/>
> </xsl:copy>
> </xsl:template>
>
> The element in this case "whatever" could have multiple http
> references and are contained in a text element of arbitrary content and
> length.


Here is an example input that I hope is what you intend to have:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>This is a text with a link: http://www.example.com/ followed by
another link: http://www.example.org/2005/04/26</p>
</root>

Then this stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xslutput method="html" indent="yes" encoding="UTF-8" />

<xsl:template match="/">
<html>
<head>
<title>regular expression example</title>
</head>
<body>
<xsl:apply-templates select="//p" />
</body>
</html>
</xsl:template>

<xsl:template match="p">
<xsl:copy>
<xsl:apply-templates select="node()" mode="make-links" />
</xsl:copy>
</xsl:template>

<xsl:template match="*" mode="make-links">
<xsl:apply-templates select="." />
</xsl:template>

<xsl:template match="text()" mode="make-links">
<xsl:analyze-string select="." regex="https?://[^\s]*">
<xsl:matching-substring>
<a href="{regex-group(0)}"><xsl:value-of select="regex-group(0)"
/></a>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

gives the following output:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>regular expression example</title>
</head>
<body>
<p>This is a text with a link: <a
href="http://www.example.com/">http://www.example.com/</a> followed by
another link: <a
href="http://www.example.org/2005/04/26">http://www.example.org/2005/04/26</a></p>
</body>
</html>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Meta-Characters, Special Characters xah@xahlee.org Java 2 05-31-2007 09:25 AM
Re: replace special characters Joseph Kesselman XML 0 01-11-2007 02:28 PM
replace special characters reynard.michel@gmail.com XML 0 01-11-2007 02:23 PM
How to convert HTML special characters to the real characters with a Java script Stefan Mueller HTML 3 07-23-2006 10:09 PM
replace special characters in string mr h q C++ 5 11-25-2003 01:21 PM



Advertisments