javaxmlxsltxsl-variable

xsl variable initialization, method doesn't get called


I'm new to xslt and have a question.

I have a validate class that contains all necessary setters and getters. For example it has such method:

public void setProducer(String producer) {
    this.producer = producer;
    System.out.println("TEST");
}

When I start my app I see that this method was not called.

I only see in console my test message, when I add in my xsl file such code:

<xsl:value-of name="producerName" />

So where is my mistake or xsl:variable initialized during first use?


I have this code:

<xsl:param name="category-name" />
<xsl:param name="subcategory-name" />
<xsl:param name="producer" />
<xsl:param name="model" />
<xsl:param name="color" />
<xsl:param name="date_of_issue" />
<xsl:param name="price" />
<xsl:param name="not_in_stock" />
<xsl:variable name="validator" select="validation:new()" />
<xsl:template match="/">
    <xsl:for-each select="products/category">
        <xsl:if test="name() = 'category' and @name=$category-name">
            <xsl:apply-templates select="subcategory[@name=$subcategory-name]" />
        </xsl:if>
    </xsl:for-each>
</xsl:template>

<xsl:template match="subcategory">
    <xsl:apply-templates select="good" />
    <xsl:variable name="errorSize"
        select="validation:getErrorListSize($validator)" />
    <xsl:if test="$errorSize = 0">
        <xsl:variable name="producerName"
            select="validation:setProducer($validator, $producer)" />

        <xsl:variable name="setModel"
            select="validation:setModel($validator, $model)" />
        <xsl:variable name="setColor"
            select="validation:setColor($validator, $color)" />
        <xsl:variable name="setDateOfIssue"
            select="validation:setDateOfIssue($validator, $date_of_issue)" />
        <xsl:if test="$not_in_stock != null">
            <xsl:variable name="setPrice"
                select="validation:setPrice($validator, $price)" />
        </xsl:if>
        <xsl:variable name="validationResult"
            select="validation:validateAllFields($validator)" />
        VALIDATION FINISHED
        <xsl:variable name="errors"
            select="validation:getErrorListSize($validator)" />
            <xsl:value-of select="$errors"/>

    </xsl:if>
    <xsl:if test="$errorSize != 0">
        REDIRECT. ERROR EXISTS
    </xsl:if>
</xsl:template>


Solution

  • Some XSLT processors such as Saxon use lazy evaluation, so the variable will not be evaluated untill the it's actually needed. So, u haven't use such approach, there are other possibilities to call methods. If variable is useless in your xsl document, use something like this instead:

    <xsl:value-of select="validation:setProducer($validator, $producer)"/>