Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 379
Aleksei Bulgak Avatar asked Apr 21 '26 16:04

Aleksei Bulgak


1 Answers

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.

like image 198
Michael Kay Avatar answered Apr 23 '26 07:04

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!