tags:

views:

48

answers:

2

Hi All,

I have a xsl parameter which a string. I want to parse that string, split it and for each substring value I want to apply the template in the xsl.

Is this possible? If so, Can you please advise an optimistic solution?

Thanks

+1  A: 

Unsure what you mean but copying this pattern may help: http://stackoverflow.com/questions/584082/xslt-best-way-to-split-and-render-comma-separated-text-as-html

Carnotaurus
Is it possible to apply the XSL template on the variable first and then the xml that needs to transformed?
Raju
+1  A: 

Edit: Missundertood the question, sorry.

The answer is yes.

Input:

<secuence>Item1 Item2 Item3</secuence>

Stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="secuence/text()" name="secuence">
        <xsl:param name="string" select="."/>
        <xsl:param name="separator" select="' '"/>
        <xsl:if test="$string != ''">
            <xsl:choose>
                <xsl:when test="contains($string,$separator)">
                    <xsl:call-template name="secuence">
                        <xsl:with-param name="string" select="substring-before($string,$separator)"/>
                        <xsl:with-param name="separator" select="$separator"/>
                    </xsl:call-template>
                    <xsl:call-template name="secuence">
                        <xsl:with-param name="string" select="substring-after($string,$separator)"/>
                        <xsl:with-param name="separator" select="$separator"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <!-- Your desired template -->
                    <Item>
                        <xsl:value-of select="$string"/>
                    </Item>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Result:

<secuence>
    <Item>Item1</Item>
    <Item>Item2</Item>
    <Item>Item3</Item>
</secuence>
Alejandro
Hi,Thanks for your quick reply. Have a question though, the xsl param will be "secuence" ? I am not sure how you were able to generate the output. I am kind of new to XSLT, Can you please advise also how to debug?Appreciate your help.ThanksRaju
Raju
@Raju: Edit answer with wrong markup for input example. The params are `$string` and `$separator`. In XSLT you can define default values for params (in this case the string value of context node and space character). Also, you ask to Carnotaurus: "Is it possible to apply the XSL template on the variable first and then the xml that needs to transformed?". In XLST 1.0, it's possible to apply templates (as with `xsl:apply-templates`) to any expression that evaluate to a node set, even a variable. In XSLT 2.0, you also can iterate over secuences (this is a new data type in XPath 2.0).
Alejandro