[xsl] multiple string replacements

Subject: [xsl] multiple string replacements
From: Xiaocun Xu <xiaocunxu@xxxxxxxxx>
Date: Thu, 6 Jun 2002 13:07:23 -0700 (PDT)
Hi,

  I am having some trouble with multiple string
replacements.  If there are two places within the
input string require string replacement, the resulting
string would be two copies of the correct result
concat together.  My suspecion is that I place the
recursion at the wrong place, therefore it is
generating a copy of the result each time it find a
string need to be replaced.  Attached are the
input/output/XSL that I have, any help in this regard
would be very much appreciated.

Much thanks,
Xiaocun
 
  My input XML:
<ePASSResponse requestID="" status="Errors">
	<Logs>
	<LogDetail AttributeName="." LogLevel="error"
LogMessage="Attribute [
ExtendedAttributeDefinitionSelectionList ] required in
element [ ExtendedAttributeDefinition:
ExtendedAttributeDefinitionDataType
ExtendedAttributeDefinitionIsRequired =
&apos;selection&apos; ]."/>
	</Logs>
</ePASSResponse>
  The result I get back:
Attribute [ Selection List ] required in element [
ExtendedAttributeDefinition: *Data Type = 'selection'
].Attribute [ Selection List ] required
 in element [ ExtendedAttributeDefinition: *Data Type
= 'selection' ].
  The name-value pair used for replacement are stored
in a XML file in format:
	<map name="ExtendedAttributeDefinitionDataType"
value="*Data Type"/>
	<map name="ExtendedAttributeDefinitionIsRequired"
value="*Required"/>
	<map name="ExtendedAttributeDefinitionSelectionList"
value="Selection List"/>
  The XSL I have:
	<!-- utility to search the given string for attribute
names and translate them to values based on given map
-->
	<saxon:function name="saxon:n2v_string">
		<xsl:param name="map" select="."/>
		<xsl:param name="value" select="."/>
		
		<!-- recursively search the map for any name matches
to be replaced by value -->
		<xsl:variable name="replaced_string">
			<xsl:for-each select="$map/maps/map">
				<xsl:if test="contains($value, @name)">
					<xsl:variable name="result">
						<xsl:call-template name="replace-string">
							<xsl:with-param name="text" select="$value"/>
							<xsl:with-param name="replace" select="@name"/>
							<xsl:with-param name="with"
select="saxon:n2v($map, @name)"/>
						</xsl:call-template>
					</xsl:variable>
					<!-- recursion -->
					<xsl:value-of select="saxon:n2v_string($map,
$result)"/>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>
		
		<!-- check replaced string; if empty, return
original string -->
		<xsl:variable name="result_string">
		<xsl:choose>
			<xsl:when test="string($replaced_string)">
				<xsl:value-of select="$replaced_string"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$value"/>
			</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>

		<saxon:return select="string($result_string)"/>
	</saxon:function>


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread