0

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

Raju
  • 114
  • 1
  • 12

2 Answers2

1

Unsure what you mean but copying this pattern may help: XSLT - Best way to split and render comma separated text as HTML

Community
  • 1
  • 1
Phil C
  • 3,687
  • 4
  • 29
  • 51
  • Is it possible to apply the XSL template on the variable first and then the xml that needs to transformed? – Raju Jul 20 '10 at 12:33
1

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">
    <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>
  • 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. Thanks Raju – Raju Jul 20 '10 at 18:29
  • @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). –  Jul 20 '10 at 19:10