Aquarius24
Aquarius24

Reputation: 1866

Tokenizer not working in xsl

I am trying to tokenize the string in xsl although it is not working, nor it is giving any error.

xsl:

<xsl:template name="checkCheckBoxValue">
        <xsl:param name="elementId" />
        <xsl:param name="mode" />
        <xsl:for-each select="/Properties/Data/Result/ValidationErrors/FieldName">
            <xsl:if test="$elementId = @name ">  
            <xsl:for-each select="tokenize(@text, ',')">
            <xsl:if test=" mode = current() ">
                <xsl:attribute name="checked">
                                    <xsl:value-of select=" 'checked' " />
                                </xsl:attribute>
                                </xsl:if>
            </xsl:for-each>


            </xsl:if>
        </xsl:for-each>
    </xsl:template>

value in @text is Train,Bus,Ferry i am passing these values individually in mode.

Upvotes: 0

Views: 175

Answers (2)

helderdarocha
helderdarocha

Reputation: 23637

I tested your XSLT template with this source:

<Properties>
    <Data>
        <Result>
            <ValidationErrors>
                <FieldName name="first" text="Train,Bus,Ferry"></FieldName>
            </ValidationErrors>
        </Result>
    </Data>
</Properties>

And placed it in a full XSLT stylesheet with a template calling your template passing some parameters to allow the if:test to run:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template match="/">
        <result>
        <xsl:call-template name="checkCheckBoxValue">
            <xsl:with-param name="elementId">first</xsl:with-param>
            <xsl:with-param name="mode" select="('Train')"/>
        </xsl:call-template>
        </result>
    </xsl:template>

    <xsl:template name="checkCheckBoxValue">
        <xsl:param name="elementId" />
        <xsl:param name="mode" />
        <xsl:for-each select="/Properties/Data/Result/ValidationErrors/FieldName">
            <xsl:if test="$elementId = @name ">  
                <xsl:for-each select="tokenize(@text, ',')">
                    <xsl:if test=" $mode = current() ">
                        <xsl:attribute name="checked">
                            <xsl:value-of select=" 'checked' " />
                        </xsl:attribute>
                    </xsl:if>
                </xsl:for-each>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

The only error I found was the mode variable which was being called inside the xsl:if without the $. Adding the $ it worked and produced the result below:

<result checked="checked"/>

If this test case matches your problem and it isn't working, the cause is probably elsewhere (in your source or in other templates that interfere with it.

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167401

I would expect an error for <xsl:if test=" mode inside of the for-each telling you the context item is a string value and that way doing mode to access a child node does not make sense.

Use <xsl:if test="$mode = .">, assuming you want to compare the mode parameter. Altogether you should be able to shorten the code to

<xsl:template name="checkCheckBoxValue">
        <xsl:param name="elementId" />
        <xsl:param name="mode" />
        <xsl:for-each select="/Properties/Data/Result/ValidationErrors/FieldName[$elementId = @name]">
            <xsl:for-each select="tokenize(@text, ',')[$mode = .]">

                <xsl:attribute name="checked">checked</xsl:attribute>

            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

Upvotes: 1

Related Questions