xsl-list
[Top] [All Lists]

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

2016-04-21 07:56:28
On 21.04.2016 14:52, Costello, Roger L. costello(_at_)mitre(_dot_)org wrote:

Okay, I changed the implementation as you suggested, see below. The new 
implementation is much shorter and much clearer. Can it be improved even 
further?

     <!--
        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="delimiter" as="xs:integer+" />
                <xsl:param name="headers" as="xs:string+" />

        <xsl:for-each select="tokenize($line, 
codepoints-to-string($delimiter))">
                <xsl:if test=". ne ''">
                        <xsl:variable name="pos" select="position()" />
                        <xsl:element name="{$headers[$pos]}">
                                <xsl:sequence select="."/>
                        </xsl:element>
                </xsl:if>
        </xsl:for-each>

     </xsl:function>


It would depend on the data and on the structure of the headers variable
whether the separate

  <xsl:if test=". ne ''">

is needed or could be moved into a predicate on

  <xsl:for-each select="tokenize($line,
codepoints-to-string($delimiter))[. ne '']">


---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>