XSL-FO display text based on percentage of value

227 Views Asked by At

I have 8 main categories. Each category has their own scores from 0-100. I need to display 5 different texts based on the percentage range of that score.

For example: Category 1 - score is 46% Show these texts when the score is between these ranges: Text 1: 0-40% Text 2: 41-60% Text 3: 61-80% Text 4: 81-90% Text 5: 91-100%

In this case, I need to display "Text 2" because 46% falls into that range.

How could I do that?

I have tried to write a code for this, but I'm not sure how to specify the percentage ranges in the template section.

XSL-FO document: <xsl:call-template name="information"> <xsl:with-param name="score" select="//attribute-lines[*/id = 'Path-Brick-Attribute']/*/value-text"/> </xsl:call-template>

Template section in the XSL document:

`<xsl:template name="information">
         <xsl:param name="score"/>
    <xsl:choose>
        <xsl:when test="$score >= 0 and 40 >=">
            <fo:block>
                <xsl:text>
                     Text 1
                </xsl:text>
            </fo:block>
        </xsl:when>
        <xsl:when test="$score &gt;= 41 and &gt;= 60">
            <fo:block>
                <xsl:text>
                    Text 2
                </xsl:text>
            </fo:block>
        </xsl:when>
    </xsl:choose>
</xsl:template>`
1

There are 1 best solutions below

0
On

The pattern you need to use is:

<xsl:template name="score-to-label">
    <xsl:param name="score"/>
    <fo:block>
        <xsl:choose>
            <xsl:when test="$score > 90">Text 5</xsl:when>
            <xsl:when test="$score > 80">Text 4</xsl:when>
            <xsl:when test="$score > 60">Text 3</xsl:when>
            <xsl:when test="$score > 40">Text 2</xsl:when>
            <xsl:otherwise>Text 1</xsl:otherwise>
        </xsl:choose>
    </fo:block>
</xsl:template>

This works because xsl:choose exits at the first test that returns true.

Note that this requires $score to be given as a number (0..100), not as a percentage.