[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] A function that tokenizes a string, wrapping each token in start-tag , end-tag pairs?


Subject: Re: [xsl] A function that tokenizes a string, wrapping each token in start-tag , end-tag pairs?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Apr 2016 05:13:34 -0000

As a single XPath 3.0 expression -- do note that no conditional is used:

for-each-pair(tokenize($lines, $delims)[not(position() = last() and .
= '')], $names, function($x,
$y){parse-xml-fragment("<"||$y||">"||$x||"</"||$y||">")/*})


A full example:
==========
let $lines :=
"1
2
3",
$names := ('a', 'b', 'c'),
$delims := "\r\n|\r|\n"
  return
     for-each-pair(tokenize($lines, $delims)[not(position() = last()
and . = '')], $names, function($x,
$y){parse-xml-fragment("<"||$y||">"||$x||"</"||$y||">")/*})

 When evaluated with Oxygen's XPath Builder, the result is:

Description: 1
XPath location: /a[1]
Start location: 1:3
Description: 2
XPath location: /b[1]
Start location: 1:3
Description: 3
XPath location: /c[1]
Start location: 1:3


Cheers,
Dimitre


On Thu, Apr 21, 2016 at 5:20 AM, 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 delimiter.
>
> 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>
> 



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.


Current Thread
Keywords