Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apply templates on each text string .... 1

Status
Not open for further replies.

simonchristieis

Programmer
Jan 10, 2002
1,144
0
0
GB
What I would like to do do apply templates on each word in a sentence.

For example,

xml
Code:
<root>
<test>One Two Three</test>
</root>


Code:
<xsl:apply-templates select="dunno where to start" mode="stuff"/>

<xsl:template match="*" mode="markup">
<xsl:if test=".='One'">Oooh One</xsl:if>
<xsl:if test=".='Two'">Tis Two</xsl:if>
<xsl:if test=".='Three'">Yippee Three</xsl:if>
</xsl:template>

Thanks in advance.

Simon
 
With v2.0, use tokenize() on text node.
 
Thanks k5tm, I hacked apart the replace templates and came up with this:

Code:
	<xsl:template match="/">
		<!-- example showing replacement of '%20' with a single space -->
		<xsl:variable name="myString" select="'bob fred harry'"/>
		<xsl:variable name="myNewString">
			<!-- go create the result tree fragment with the replacement -->
			<xsl:call-template name="SubstringReplace">
				<xsl:with-param name="stringIn" select="$myString"/>
				<xsl:with-param name="splitOn" select="' '"/>
			</xsl:call-template>
		</xsl:variable>
		<!--
		show the new string. concat() will treat the result tree
		fragment as if it were a string returned by the string() function.
		-->
		<xsl:value-of select="$myNewString"/>
	</xsl:template>

	<!-- here is the template that does the replacement -->
	<xsl:template name="SubstringReplace">
		<xsl:param name="stringIn"/>
		<xsl:param name="splitOn"/>
		<xsl:choose>
			<xsl:when test="contains($stringIn,$splitOn)">
				<xsl:call-template name="ShowThis">
					<xsl:with-param name="String" select="substring-before($stringIn,$splitOn)"/>
				</xsl:call-template>
				
				<xsl:call-template name="SubstringReplace">
					<xsl:with-param name="stringIn" select="substring-after($stringIn,$splitOn)"/>
					<xsl:with-param name="splitOn" select="$splitOn"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="ShowThis">
					<xsl:with-param name="String" select="$stringIn"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template name="ShowThis">
		<xsl:param name="String"/>


	---	<xsl:value-of select="$String"/> ---


		</xsl:apply-templates>
		
	</xsl:template>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top