XSLT 2.0 replace < > special characters

M

mattmozer

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
 
M

Martin Honnen

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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:eek:utput 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top