0

I have a block of xslt that examines if the current node and performs some calculations using different lookup fields if a certain field matches a certain text string. It works as expected and is reasonably fast. I am fairly new to XSLT and would like to optimize this template as much as possible, hopefully so its only a few lines and not the same block repeating over and over with only the variable being different. Example of the code:

<td class="jobRow centerCell" colspan="1">
                <xsl:if test="$fieldvalue = 'RN'">
                    <xsl:choose>
                        <xsl:when test="/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x007 = ''">
                            <span>0</span>
                            <br/>
                            <span style="font-size:8px;color:red">No budget assigned!</span>
                        </xsl:when>
                        <xsl:otherwise>
                            <span style="padding-left:20px;color:black;font-weight:bold;font-size:12px;">
                                <xsl:value-of select="format-number(/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x007,'0.0')"/>
                            </span>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
                <xsl:if test="$fieldvalue = 'ADM SEC'">
                    <xsl:choose>
                        <xsl:when test="/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x001 = ''">
                            <span>0</span>
                            <br/>
                            <span style="font-size:8px;color:red">No budget assigned!</span>
                            <xsl:variable name="JobBudget">0</xsl:variable>
                        </xsl:when>
                        <xsl:otherwise>
                            <span style="padding-left:20px;color:black;font-weight:bold;font-size:12px;">
                                <xsl:value-of select="format-number(/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x001,'0.0')"/>
                            </span>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
                <xsl:if test="$fieldvalue = 'CHG RN'">
                    <xsl:choose>
                        <xsl:when test="/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x002 = ''">
                            <span>0</span>
                            <br/>
                            <span style="font-size:8px;color:red">No budget assigned!</span>
                            <xsl:variable name="JobBudget">0</xsl:variable>
                        </xsl:when>
                        <xsl:otherwise>
                            <span style="padding-left:20px;color:black;font-weight:bold;font-size:12px;">
                                <xsl:value-of select="format-number(/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x002,'0.0')"/>
                            </span>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
                <xsl:if test="$fieldvalue = 'PCA'">
                    <xsl:choose>
                        <xsl:when test="/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x005 = ''">
                            <span>0</span>
                            <br/>
                            <span style="font-size:8px;color:red">No budget assigned!</span>
                            <xsl:variable name="JobBudget">0</xsl:variable>
                        </xsl:when>
                        <xsl:otherwise>
                            <span style="padding-left:20px;color:black;font-weight:bold;font-size:12px;">
                                <xsl:value-of select="format-number(/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x005,'0.0')"/>
                            </span>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:if>

As you can see, the action occurs within a table cell and first tests if the text is a match, and when it is performs a calculation. The only difference between each calculation is the lookup field reference.

I am working on some experimentation and will post with anything I come up. Any help or direction is appreciated! Thanks

1 Answer 1

0

As you correctly noticed at least the repeating block could be refactored.

For example, declare the following Xsl template for rendering Budget:

<xsl:template name="RenderBudget">
    <xsl:param name="CostCenter"/>

    <xsl:choose>
        <xsl:when test="$CostCenter = ''">
            <span>0</span>
            <br/>
            <span style="font-size:8px;color:red">No budget assigned!</span>
        </xsl:when>
        <xsl:otherwise>
            <span style="padding-left:20px;color:black;font-weight:bold;font-size:12px;">
             <xsl:value-of select="format-number($CostCenter,'0.0')"/>
            </span>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

and then invoke it

<xsl:call-template name="RenderBudget">
    <xsl:with-param name="CostCenter" select="/dsQueryResponse/Rows/Row[@Job_x0020_Title = $fieldvalue and substring-after(@Test_x0020_Cost_x0020_Center.,'#') = $jobvalue]/@Test_x0020_Cost_x0020_Center_x002"/>
</xsl:call-template>

Note:the code has not been tested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.