|
|
|
date: Thu, 17 Jan 2008 22:06:04 -0800 (PST),
group: microsoft.public.xsl
back
multy string replace
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>
date: Thu, 17 Jan 2008 22:06:04 -0800 (PST)
author: unknown
Re: multy string replace
alexseys@gmail.com wrote:
> Well you are talking to complete novice in xml/xsl that took some
> examples slaped it toghether in hope it works, it does for the most
> part but got to get rid of that commant word that screws up IE.
XSLT 1.0 was standardized in 1999 and there are lots of implementations
by now, IE 6 and later use MSXML 3, Firefox/Mozilla use Transformiix,
Opera has its own XSLT 1.0 processor too, Safari uses libxslt I think.
Then since 2007 we have XSLT 2.0 so far supported by three processors,
Saxon from <http://saxon.sourcforge.net/>, Altova from
<http://www.altova.com/>, and Gestalt.
You will need to decide whether you want to use XSLT 2.0 or XSLT 1.0,
XSLT 2.0 makes lots of things easier as it has support for grouping,
allows to write functions, has a lot of more string processing functions
and has regular expression support.
Unless you want to perform your XSLT transformations in a browser you
could start with XSLT 2.0 by installing one of the three processors that
support it.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
date: Fri, 18 Jan 2008 18:59:54 +0100
author: Martin Honnen
|
|