Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

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>

Upvotes: 3

Views: 883

Answers (2)

bsiamionau
bsiamionau

Reputation: 8229

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)"/>

Upvotes: 2

Michael Kay
Michael Kay

Reputation: 163438

First point: extension functions in XSLT are highly product-dependent, so you can't really ask us this question without telling us what XSLT processor you are using.

Mixing a declarative language and a procedural language is always going to be tricky (and the best thing would be to avoid it). Certainly in XSLT an optimizing processor will evaluate variables lazily, which means that if a variable isn't referenced then it won't be evaluated.

In Saxon you can generally get away with calling Java methods that have side-effects if you "pretend" to use the result of the method in your result tree; you can achieve that by calling the method from an xsl:value-of instruction.

Upvotes: 4

Related Questions