[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: A function that tokenizes a string, wrapping each

Subject: Re: A function that tokenizes a string, wrapping each token in start-tag , end-tag pairs?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 21 Apr 2016 15:13:10 -0000
Re:  A function that tokenizes a string
Why not use xsl:analyze-string?

<xsl:analyze-string select="$line" regex="....">
    <xsl:variable name="p" select="position()"/>
    <xsl:element name="{$tag[$p]}"><xsl:value-of select="."/></xsl:element>

For reusability in XSLT 3.0 this is a good candidate for a
higher-order-function - you could pass a parameter which is a function to be
called to determine the element name to be used for the Nth token.

Michael Kay

> On 21 Apr 2016, at 13:20, Costello, Roger L. costello@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> Hi Folks,
> I want a function that tokenizes a string and wraps each token in a
start-tag, end-tag pair.
> I figure that I should pass into the function the string to be tokenized and
a sequence of integers corresponding to the decimal value(s) of the token
> Also, I figure that I should pass into the function a sequence of strings,
corresponding to the tag names to be used for wrapping each token.
> I've implemented the function, see below. It works fine, but I want to know
if it can be improved. Is there a way to write the function more idiomatic?
Shorter? Generalized to be more widely useful?  /Roger
>    <!--
>    	Create an element for each non-empty token in $line.
>    	$line is tokenized using the sequence of symbols denoted
>    	by $line-delimiter.
>    	For the token at position i, name the element using the
>    	string in headers[$i]
>   -->
>    <xsl:function name="f:line" as="element()*">
>    	<xsl:param name="line" as="xs:string" />
>    	<xsl:param name="line-delimiter" as="xs:integer+" />
>        	<xsl:param name="headers" as="xs:string+" />
>    	<xsl:variable name="tokens" select="tokenize($line,
codepoints-to-string($line-delimiter))" as="xs:string*" />
>        	<xsl:variable name="len" select="count($tokens)" as="xs:integer" />
>        	<xsl:for-each select="1 to $len">
>            		<xsl:variable name="index" select="xs:integer(.)"
as="xs:integer" />
>            		<xsl:variable name="value" select="$tokens[position() eq
$index]" as="xs:string" />
>            		<xsl:choose>
>                		<xsl:when test="$value eq ''"/>
>                		<xsl:otherwise>
>                			<xsl:element name="{$headers[position() eq $index]}">
>                        				<xsl:sequence select="$value"/>
>                    			</xsl:element>
>                		</xsl:otherwise>
>            		</xsl:choose>
>        	</xsl:for-each>
>    </xsl:function>

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.