Anuja Lamahewa
Anuja Lamahewa

Reputation: 937

How to do a calculation on XSL

I have a xml file like this:

<RESULTS>
    <COMPONENT>
        <COMPONENT_NAME>APP1</COMPONENT_NAME>
        <COMPONENT_TOTAL>26</COMPONENT_TOTAL>
        <COMPONENT_TESTED>4</COMPONENT_TESTED>
    <COMPONENT>

    <COMPONENT>  
         <COMPONENT_NAME>APP2</COMPONENT_NAME>
         <COMPONENT_TOTAL>10</COMPONENT_TOTAL>
         <COMPONENT_TESTED>9</COMPONENT_TESTED>
    </COMPONENT>
</RESULTS>

and this is my current xsl C# code:

     StringBuilder sb = new StringBuilder();                   

     sb.AppendLine("<xsl:for-each select='RESULTS/COMPONENT'>");

     sb.AppendLine("<xsl:value-of select='COMPONENT_NAME'/>    " + " = " + "
                    <xsl:value-of select='COMPONENT_TESTED'/>  " + " / " + " 
                    <xsl:value-of select='COMPONENT_TOTAL'/>") ;

it gives; APP1 = 4 / 26 APP2 = 9 / 10

I want to do a calcualtion. (tested/total)*100

so for APP2 (9/10)*100 = 90 "%"

How can I do that ?

So far I tried these,

string a = "<xsl:value-of select='COMPONENT_NAME'/>"; // not working

sb.AppendLine("<xsl:value-of select= " + "\"" + "sum (COMPONENT/[@'COMPONENT_TESTED'] / COMPONENT/[@'COMPONENT_TOTAL']) " + "\"" + "   />");

sb.AppendLine("<xsl:name='tested' select='COMPONENT_TESTED'/>" + <xsl:name='total'  select='COMPONENT_TOTAL'/>"

none of them work :(

Upvotes: 3

Views: 100

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

Try:

<xsl:value-of select='COMPONENT_TESTED div COMPONENT_TOTAL * 100'/>

or, IMHO more correctly:

<xsl:value-of select='format-number(COMPONENT_TESTED div COMPONENT_TOTAL, "#%")'/>

Upvotes: 1

Related Questions