XSLT 2.0 replace < > special characters

Discussion in 'XML' started by mattmozer@gmail.com, Apr 25, 2005.

  1. Guest

    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
     
    , Apr 25, 2005
    #1
    1. Advertising

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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 26, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    33,137
    Stefan Mueller
    Jul 23, 2006
  2. mr h q
    Replies:
    5
    Views:
    15,223
    Karl Heinz Buchegger
    Nov 25, 2003
  3. Joseph Kesselman

    Re: replace special characters

    Joseph Kesselman, Jan 11, 2007, in forum: XML
    Replies:
    0
    Views:
    488
    Joseph Kesselman
    Jan 11, 2007
  4. Replies:
    0
    Views:
    508
  5. Replies:
    2
    Views:
    1,114
    Ingo Menger
    May 31, 2007
Loading...

Share This Page