Reputation: 147
my xsl variable:
<xsl:variable name="string">Satur - Sun - Mon - Tues - Wednes - Thurs - Fri</xsl:variable>
i need to translate or replace the string
variable data from:
Satur - Sun - Mon - Tues - Wednes - Thurs - Fri
so, the output should be like this:
Saturday - Sunday - Monday - Tuesday - Wednesday - Thursday - Friday
................................
@Mads Hansen
i am newbie in xsl
and xml
and i tried your code but it not works
100%
it is wrong
my xsl file:
<xsl:variable name="string">Satur - Sun - Mon - Tues - Wednes - Thurs - Fri</xsl:variable>
<xsl:template match="/">
<html>
<body>
<h2>result: </h2>
<xsl:value-of
select="for $token in tokenize($string, '\s-\s')
return(
document('')/*/config/replacement[find[.=$token]]/replace,
$token
)[1]"
separator=" - "
/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
my xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<config>
<replacement>
<find>Satur</find>
<replace>Saturday</replace>
</replacement>
<replacement>
<find>Sun</find>
<replace>Sunday</replace>
</replacement>
<replacement>
<find>Mon</find>
<replace>Monday</replace>
</replacement>
<replacement>
<find>Tues</find>
<replace>Tuesday</replace>
</replacement>
<replacement>
<find>Wednes</find>
<replace>Wednesday</replace>
</replacement>
<replacement>
<find>Thurs</find>
<replace>Thursday</replace>
</replacement>
<replacement>
<find>Fri</find>
<replace>Friday</replace>
</replacement>
</config>
what is wrong in my codes ?
Upvotes: 2
Views: 10466
Reputation: 243459
I. XPath 2.0 (XSLT 2.0) solution:
Use the following XPath 2.0 one-liner:
concat(replace(.,'\s+-\s*', 'day - '), 'day')
XSLT - based verification:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:sequence select="concat(replace(.,'\s+-\s*', 'day - '), 'day')"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>Satur - Sun - Mon - Tues - Wednes - Thurs - Fri</t>
the wanted, correct result is produced:
Saturday - Sunday - Monday - Tuesday - Wednesday - Thursday - Friday
II. XSLT 1.0 solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:call-template name="replace">
<xsl:with-param name="pS" select=
"concat(normalize-space(translate(., '-', ' ')), 'day')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="pS"/>
<xsl:param name="pTarget" select="' '"/>
<xsl:param name="pReplacement" select="'day - '"/>
<xsl:if test="$pS">
<xsl:value-of select="substring-before(concat($pS,$pTarget), $pTarget)"/>
<xsl:if test="contains($pS, $pTarget)">
<xsl:value-of select="$pReplacement"/>
</xsl:if>
<xsl:call-template name="replace">
<xsl:with-param name="pS" select="substring-after($pS, $pTarget)"/>
<xsl:with-param name="pTarget" select="$pTarget"/>
<xsl:with-param name="pReplacement" select="$pReplacement"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document (above), the same wanted, correct result is produced:
Saturday - Sunday - Monday - Tuesday - Wednesday - Thursday - Friday
Upvotes: 3
Reputation: 27994
A variant on @Mads' answer:
<xsl:template match="/">
<xsl:value-of
select="for $token in tokenize($string, '\s-\s')
return concat($token, 'day')"
separator=" - " />
</xsl:template>
Then you can omit the <my:config>
.
Upvotes: 2
Reputation: 66723
An XSLT 2.0 solution:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:my="http://stackoverflow.com/questions/12502665/xsl-how-to-translate-or-replace-string">
<xsl:output indent="yes"/>
<xsl:variable name="string">Satur - Sun - Mon - Tues - Wednes - Thurs - Fri</xsl:variable>
<my:config>
<replacement>
<find>Satur</find>
<replace>Saturday</replace>
</replacement>
<replacement>
<find>Sun</find>
<replace>Sunday</replace>
</replacement>
<replacement>
<find>Mon</find>
<replace>Monday</replace>
</replacement>
<replacement>
<find>Tues</find>
<replace>Tuesday</replace>
</replacement>
<replacement>
<find>Wednes</find>
<replace>Wednesday</replace>
</replacement>
<replacement>
<find>Thurs</find>
<replace>Thursday</replace>
</replacement>
<replacement>
<find>Fri</find>
<replace>Friday</replace>
</replacement>
</my:config>
<xsl:template match="/">
<xsl:value-of
select="for $token in tokenize($string, '\s-\s')
return
(
document('')/*/my:config/replacement[find[.=$token]]/replace,
$token
)[1]"
separator=" - " />
</xsl:template>
</xsl:stylesheet>
Upvotes: 2