Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] Tracking position in recursive functions?

From: Emmanuel B&#xE9;gu&#xE9; <eb@---------->
To:
Date: 4/2/2009 7:39:00 AM
Hello,

You need to recursively send a "position" parameter to increment the
position.

If you simplify your template thusly:

<xsl:template name="str:_tokenize-delimiters">
	<xsl:param name="args"/>
	<xsl:param name="delimiters"/>
	<xsl:param name="pos" select="1"/>

	<xsl:variable name="delimiter" select="substring($delimiters, 1, 1)"/>
	<xsl:variable name="left">
		<xsl:choose>
			<xsl:when test="not(contains($args,$delimiter))">
				<xsl:value-of select="$args"/>
				</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="substring-before($args,$delimiter)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
	<xsl:variable name="right" select="substring-after($args,$delimiter)"/>
	<xsl:if test="$left != ''">
		<option pos="{$pos}"><xsl:value-of select="$left"/></option>
		</xsl:if>
	<xsl:if test="$right != ''">
		<xsl:call-template name="str:_tokenize-delimiters">
			<xsl:with-param name="args" select="$right"/>
			<xsl:with-param name="delimiters" select="$delimiters"/>
			<xsl:with-param name="pos" select="$pos + 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

(with just one recursive call instead of three, and just one writing
of the <option> element)

you get:
for the source string ('args'): one two three four
and the space delimiter
the following result:

	<option pos="1">one</option>
	<option pos="2">two</option>
	<option pos="3">three</option>
	<option pos="4">four</option>

Regards,
EB

> -----Original Message-----
> From: himanshu padmanabhi [mailto:himanshu.padmanabhi@xxxxxxxxx]
> Sent: Thursday, April 02, 2009 8:56 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Tracking position in recursive functions?
>
>
> Hi,
>
> These is just the part of str::tokenize.
>
> I just want to ask how to track "position" in recursive functions like
> "str:_tokenize-delimiters"?
>
> "position()" isn't solving the purpose.
>
> "$args" value is "-p 10 -t 20 abcd.domain.com" which I want to
> tokenize by space.
>
> What my final goal is,
> If called with position()=1 then print 2nd pos = 10
> If called with position()=2 then print 4nd pos = 20
> If called with position()=3 then print 5nd pos = abcd.domain.com
> (There are 3 xml elements so str:tokenize can be called 3 times or I
> call it 1 time and save 2,4,5 values in variables and can print it in
> calling template.)
>
> Currently it is printing all the tokens each time.
> Tracking 'position' in recursive function also can give me answer like
> 2nd,4th,5th position token.
>
> <xsl:stylesheet version="1.0"
>                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                 xmlns:str="http://exslt.org/strings"
>                 extension-element-prefixes="str">
>
> <xsl:template name="str:_tokenize-delimiters">
>   <xsl:param name="args" />
>   <xsl:param name="delimiters" />
>
>   <xsl:variable name="delimiter" select="substring($delimiters, 1, 1)" />
>
>   <xsl:choose>
>         <xsl:when test="not($delimiter)" >
>             <option><xsl:value-of select="$args" /></option>
>         </xsl:when>
>
>     <xsl:when test="contains($args, $delimiter)">
>         <option><xsl:value-of select="substring-after($args,
> $delimiter)" /></option>
>         <xsl:if test="not(starts-with($args, $delimiter))">
>             <xsl:call-template name="str:_tokenize-delimiters">
>                 <xsl:with-param name="args"
> select="substring-before($args, $delimiter)" />
>                 <xsl:with-param name="delimiters"
> select="substring($delimiters, 2)" />
>             </xsl:call-template>
>         </xsl:if>
>
>         <xsl:call-template name="str:_tokenize-delimiters">
>             <xsl:with-param name="args" select="substring-after($args,
> $delimiter)" />
>             <xsl:with-param name="delimiters" select="$delimiters" />
>         </xsl:call-template>
>     </xsl:when>
>
>     <xsl:otherwise>
>         <xsl:call-template name="str:_tokenize-delimiters">
>             <xsl:with-param name="args" select="$args" />
>             <xsl:with-param name="delimiters"
> select="substring($delimiters, 2)" />
>         </xsl:call-template>
>     </xsl:otherwise>
>   </xsl:choose>
> </xsl:template>
>
> </xsl:stylesheet>
> ---------------------------------
> Thanks and Regards,
> Himanshu Padmanabhi


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent