Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries [Thread Prev] >Thread Next - Re: multy string replace multy string replaceTo: NULL Date: 1/17/2008 10:06:00 PM I have xml and xsl files provide below
My goal isto replace some strings in the xml whle conerting to html
I can replace one string at a time with the following anyone can help
me to replace more than that?
i.e. ** to be replaced with </BR>
and word "comment" to be replaced with a "-"
Any help appeciated
Thank you
<?xml version="1.0"?>
<!DOCTYPE computer[<!ELEMENT computer (hostname,idLocation*, datetime,
component*)><!ELEMENT component (type,name,attr*)><!ELEMENT hostname
(#PCDATA)><!ELEMENT datetime (#PCDATA)><!ELEMENT type (#PCDATA)><!
ELEMENT name (#PCDATA)><!ELEMENT idLocation (#PCDATA)><!ELEMENT attr
(name,value)><!ELEMENT value (#PCDATA)>]>
<computer>
<hostname>WHATEVER</hostname>
<idLocation>$$$$$</idLocation>
<datetime>1/15/2000 4:10:53 PM</datetime>
<component>
<type>Network Files</type>
<name>networks</name>
<attr>
<name>File Content</name>
<value>**# <network name> <network
number> [aliases...] [#<comment>]
**
</value>
</attr>
</component>
and this xsl file to format and replace strings
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xs fn">
<xsl:key name='type' match='type' use='.'/>
<xsl:output method="html" indent="yes" />
<!-- reusable replace-string function -->
<xsl:template name="replace-string" >
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:variable name="before" select="substring-before($text,
$from)"/>
<xsl:variable name="after" select="substring-after($text,
$from)"/>
<xsl:variable name="prefix" select="concat($before, $to)" />
<xsl:value-of select="$before" disable-output-escaping="yes"/>
<xsl:value-of select="$to" disable-output-escaping="yes"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<html>
<head>
<style type="text/css">
</style>
<Title>
<xsl:value-of select="computer/hostname"/>
</Title>
<h1>
<xsl:value-of select="computer/hostname"/>
</h1>
<Title>
<xsl:value-of select="computer/hostname"/>
</Title>
</head>
<body>
<xsl:for-each select="//type[generate-id()=generate-
id(key('type',.)[1])]">
<xsl:sort select="."/>
<li>
<a class="link">
<xsl:attribute name="href">
#<xsl:value-of select="."/>
</xsl:attribute>
<xsl:value-of select="."/>
</a>
</li>
</xsl:for-each>
<table>
<tbody>
<xsl:for-each select="computer/component">
<tr>
<th>
<a>
<xsl:attribute name="name">
<xsl:apply-templates select="type"/>
</xsl:attribute>
<xsl:apply-templates select="type"/>
</a>
</th>
<td>
<xsl:apply-templates select="name"/>
</td>
</tr>
<xsl:for-each select="attr">
<tr>
<td>
<xsl:apply-templates select="name"/>
</td>
<td>
<xsl:variable name="yourstring" select="value"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$yourstring"/
>
<xsl:with-param name="from" select="'**'"/>
<xsl:with-param name="to" select="'<BR>'"/
>
</xsl:call-template>
</td>
</tr>
<tr></tr>
<tr></tr>
</xsl:for-each>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
