Lenka
Lenka

Reputation: 75

Concatenation after tokenize using XSL

I have a string input, from which I wanted to get some specific value and concat them. By far I am able to tokenize them. But how to concat them? Here is my input string:

delta=43; domain=.com; path=/; secure, id=34e; path=/, alpha=21; path=/ 

This is my XSL:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:regexp="http://exslt.org/regular-expressions"    xmlns:dp="http://www.datapower.com/extensions" xmlns:dyn="http://exslt.org/dynamic"     extension-element-prefixes="dp regexp" xmlns:str="http://exslt.org/strings"     exclude-result-prefixes="dp dyn str">
    <xsl:output method="xml" encoding="utf-8" indent="no" />
    <xsl:template match="/">
        <xsl:variable name="adVal" select="'delta=43; domain=.com; path=/; secure, id=34e; path=/, alpha=21; path=/'" />
        <xsl:variable name="tokenizedadVal" select="str:tokenize($adVal, ',' )" />      
    </xsl:template>
</xsl:stylesheet>

After using the I can see the output as below:

<token>delta=43; domain=delta.com; path=/; secure</token>
<token>id=34e; path=/</token>
<token>alpha=21; path=/</token>

What I want now, is to concat the name value pair for delta, id & alpha and put it into a variable. So that the xsl variable will hold the value: delta=43;id=34e;alpha=21 in it.

Upvotes: 0

Views: 1243

Answers (2)

Alex Fomin
Alex Fomin

Reputation: 505

I suggest to use separate template to perform your tokenize, concat and format of string as required (avoiding to use full XSLT 2.0 processor), please see XSL below:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="no" />

    <xsl:template name="str-split">
        <xsl:param name="param.str" /> <!-- input string-->
        <xsl:param name="param.sep" /> <!-- block separator-->
        <xsl:param name="param.fval.before" /> <!--special character before which value will be taken-->
        <!--build required string-->
        <xsl:variable name="var.str.final">
            <xsl:if test="string-length($param.str) > 0">
                <xsl:variable name="var.item" select="substring-before(concat($param.str, $param.sep), $param.sep)"/>
                <xsl:value-of select="concat(substring-before(normalize-space($var.item), $param.fval.before), ' ')"/>          
                <xsl:call-template name="str-split">
                    <xsl:with-param name="param.str" select="substring-after($param.str, $param.sep)"/>
                    <xsl:with-param name="param.sep" select="$param.sep"/>
                    <xsl:with-param name="param.fval.before" select="$param.fval.before"/>
                </xsl:call-template>    
            </xsl:if>    
        </xsl:variable>   
        <!--format required string-->
        <xsl:value-of select="translate(normalize-space($var.str.final), ' ', $param.fval.before)"/>
    </xsl:template>

    <xsl:template match="/">
        <xsl:variable name="adVal" select="'delta=43; domain=.com; path=/; secure, id=34e; path=/, alpha=21; path=/'" />
        <!--as you require use call-template inside of variable to store your formatted string-->
        <xsl:variable name="yourFinalString"> 
            <xsl:call-template name="str-split">
                <xsl:with-param name="param.str" select="$adVal"/>
                <xsl:with-param name="param.sep" select="','"/>
                <xsl:with-param name="param.fval.before" select="';'"/>
            </xsl:call-template>
        </xsl:variable>
       <!--simply run your variable inside token block, or run without block--> 
        <token>
            <xsl:value-of select="$yourFinalString"/>
        </token>

    </xsl:template>
</xsl:stylesheet> 

Then result (as expected):

<token>delta=43;id=34e;alpha=21</token>

Upvotes: 1

Tim C
Tim C

Reputation: 70618

If you could use a full XSLT 2.0 processor, you could do something like this...

<xsl:value-of select="for $i in $tokenizedadVal return substring-before($i, ';')" separator=";" />

But if not, you probably have to do something like this...

<xsl:for-each select="$tokenizedadVal">
    <xsl:if test="position() > 1">;</xsl:if>
    <xsl:value-of select="substring-before(., ';')" />    
</xsl:for-each>

Upvotes: 1

Related Questions