[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Search and Replace Help


Subject: [xsl] Search and Replace Help
From: Matthew Ebbertt <mje004@xxxxxxxx>
Date: Sat, 19 May 2007 14:28:32 -0400

I'm new (as in just started working on it last night) to the whole XML and
XSL thing.  What I am trying to do relates to the popular vBulletin message
boards where you can download private messages in XML format.  I've created
an XSL that transforms the XML into something infinitely more readable, but
I've run into a snag trying to do a search and replace.  As shown below,
there are tags that get exported of the form [quote=name]...[/quote].  What
I'm trying to do is replace every instance of that with XHTML code that goes
<blockquote><i>Originally posted by: <b>name</b></i>...</blockquote>.  I've
tried using the script posted at
http://topxml.com/snippetcentral/main.asp?view=viewsnippet&lang=xsl&id=v2001
0223112713 but even that won't work in the example he used (it simply writes
out <br /> and displays that, instead of it doing a line feed as the <br />
tag is supposed to do).  I haven't even tried to manipulate it more after I
ran into that problem.  I have no experience with any programming, just good
old HTML type stuff, so this is all new territory for me.  Here are examples
of what I'm working with.

The XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="pms.xsl"?>

<privatemessages>
    <folder name="Sent Items">
        <privatemessage>
            <datestamp>2007-02-14 09:09</datestamp>
            <title>Re: Heroes</title>
            <fromuser>jack</fromuser>
            <fromuserid>112</fromuserid>
            <touser>john</touser>
            <message><![CDATA[[QUOTE=jack][QUOTE=john]How you
doing?[/QUOTE]

Good.  You?[/QUOTE]

Cool.]]></message>
        </privatemessage>
    </folder>
</privatemessages>

The XSL (pms.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
 
 <xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<style type="text/css">
blockquote {border: thin solid black;}
body {background-color: #DCDCDC;font-family: Arial, Helvetica,
sans-serif;font-size: 12px;color: #336699;}
td, th {font-family: Arial, Helvetica, sans-serif;font-size:
12px;line-height: 24px;color: #333333;}
a {color: #3366CC;text-decoration: none;}
a:hover {text-decoration: underline;}
a:link {color: #0000FF;}
a:visited {color: #3366FF;}
a:hover {text-decoration: none;}
a:active {color: #FF0000;}
hr {border: 0; width: 90%; height: 2px; background-color: #000000; color:
#000000;}
TABLE {width: 100%; border:thin solid black; border-collapse:collapse;
empty-cells:hide;}
TD {border: thin solid black;}
</style>
<title><xsl:for-each select="privatemessages/folder"><xsl:value-of
select="@name" /></xsl:for-each></title>
</head>
<body>
<xsl:for-each select="privatemessages/folder/privatemessage">
    <xsl:text>From: </xsl:text><a
href="http://www.website.com/vb/member.php?u={fromuserid}"><xsl:value-of
select="fromuser"/></a><br />
    <xsl:text>To: </xsl:text> <xsl:value-of select="touser"/><br />
    <xsl:text>On: </xsl:text> <xsl:value-of select="datestamp"/><br />
    <xsl:text>Subject: </xsl:text> <xsl:value-of select="title"/><br />
    <table>
    <tr>
    <td valign="top" width="60px" style="text-align:
center;"><xsl:text>Message:</xsl:text></td>
    <td>
    <xsl:call-template name="replace-text">
      <xsl:with-param name="text" select="message"/>
      <xsl:with-param name="replace" select="'&#10;'"/>
      <xsl:with-param name="by" select="'&lt;br /&gt;'"/>
    </xsl:call-template>
    </td>
    </tr>
    </table>
    <hr size="4" />
</xsl:for-each>
</body>
</html>

</xsl:template>
<!-- template that does a search & replace -->
<xsl:template name="replace-text">
   <xsl:param name="text"/>
   <xsl:param name="replace" />
   <xsl:param name="by"  />

   <xsl:choose>
   <xsl:when test="contains($text, $replace)">
      <xsl:value-of select="substring-before($text, $replace)"/>
      <xsl:value-of select="$by" disable-output-escaping="yes"/>
      <xsl:call-template name="replace-text">
         <xsl:with-param name="text" select="substring-after($text,
$replace)"/>
         <xsl:with-param name="replace" select="$replace" />
         <xsl:with-param name="by" select="$by" />
      </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
      <xsl:value-of select="$text"/>
   </xsl:otherwise>
   </xsl:choose>

</xsl:template>
</xsl:stylesheet>

The output (just the problem area):
[QUOTE=jack][QUOTE=john]How you doing?[/QUOTE]<br /><br />Good.
You?[/QUOTE]<br /><br />Cool.

So the two problems are that 1) it is writing out/displaying <br /> instead
of doing a line break and 2) I need to replace [QUOTE=jack] & [QUOTE=john]
with <blockquote><i>Originally posted by: <b>jack (or john)</b></i> (while
allowing for the fact that there may be a mary, jake, etc.) and [/QUOTE]
with </blockquote>.

Any ideas?


Current Thread
Keywords