XSLT Select Text; Add href

Discussion in 'XML' started by joealonso@gmail.com, May 19, 2005.

  1. Guest

    Problem:

    I have an xml news feed:

    blah blah yesterday by the news from General Motors (NYSE:GM) that
    helped bring the market to a three-week high blah blah We all know that
    the old days are gone, when IBM (NYSE:IBM) employees wore dark suits
    and ties, and white shirts, worked nine to five blah Procter & Gamble
    (NYSE:pG)

    I need to select the text between "NYSE:" and ")" and add <a
    href="page.asp?symbol=IBM">$string</a>

    does anyone know how to do this? I've got an extra Half-Life2 serial
    number for the first person to figure it out.

    I've tried something like...

    <xsl:template name="links">
    <xsl:param name="str"/>
    <xsl:choose>
    <xsl:when test="contains($str,'NYSE')">
    <a href="#id{substring-after($str,'NYSE')}"><xsl:value-of
    select="substring-after($str,'NYSE')"/></a>
    <xsl:call-template name="links">
    <xsl:with-param name="str" select="substring-after($str,'NYSE')
    and substring-before($str,')')"/>
    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$str"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    Thanks in advance!
     
    , May 19, 2005
    #1
    1. Advertising

  2. Guest

    Nevermind I figured it out. Below is the code...

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" version="4.01" encoding="iso-8859-1"
    indent="yes" />

    <xsl:template name="hyperlink">
    <xsl:param name="string" select="string()" />
    <xsl:choose>
    <xsl:when test="contains($string, '(NYSE:') and contains($string,
    ')')">
    <xsl:variable name="url"
    select="substring-after(substring-before($string, ')'), '(NYSE:')" />
    <xsl:variable name="rest" select="substring-after($string,
    ')')"/>
    <xsl:variable name="before" select="substring-before($string,
    $url)"/>
    <xsl:value-of select="$before"/>
    <a href="/test.asp?symbol={$url}"><xsl:value-of
    select="$url"/></a>)
    <xsl:call-template name="hyperlink">
    <xsl:with-param name="string" select="$rest" />
    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$string" />
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>


    <xsl:template match="/response/content_servlet_results/result">
    <h5><xsl:value-of select="Headline" /></h5>
    <xsl:for-each select="Story/P">
    <p>
    <xsl:call-template name="hyperlink">
    <xsl:with-param name="string" select="."/>
    </xsl:call-template></p>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

    - Joe Alonso
     
    , May 19, 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. Michael Reiche
    Replies:
    3
    Views:
    10,965
    Michael Reiche
    Feb 5, 2004
  2. CRON
    Replies:
    24
    Views:
    203,770
    Adrienne Boswell
    Jun 20, 2006
  3. Soren Vejrum
    Replies:
    4
    Views:
    763
    Lasse Reichstein Nielsen
    Jul 5, 2003
  4. saiho.yuen
    Replies:
    3
    Views:
    549
    kaeli
    Sep 14, 2004
  5. David
    Replies:
    1
    Views:
    347
    David
    Dec 6, 2006
Loading...

Share This Page