Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9089567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:10:52+00:00 2026-06-16T22:10:52+00:00

I need to calculate a simple weighted average in a XForms form. How can

  • 0

I need to calculate a simple weighted average in a XForms form. How can I do that in an elegant and declarative way using XPath and/or XQuery ?

[EDITED] This is the source XML document :

<Examens>
    <Examen>
        <ExamenId>1</ExamenId>
        <Coef>1</Coef>
        <Notes>
            <Note>
                <EleveId>100</EleveId>
                <Valeur>4</Valeur>
            </Note>
            <Note>
                <EleveId>101</EleveId>
                <Valeur>4.2</Valeur>
            </Note>
            <Note>
                <EleveId>102</EleveId>
                <Valeur>3.8</Valeur>
            </Note>
        </Notes>
    </Examen>
    <Examen>
        <ExamenId>2</ExamenId>
        <Coef>2</Coef>
        <Notes>
            <Note>
                <EleveId>100</EleveId>
                <Valeur>5</Valeur>
            </Note>
            <Note>
                <EleveId>101</EleveId>
                <Valeur/>
            </Note>
            <Note>
                <EleveId>102</EleveId>
                <Valeur>3.5</Valeur>
            </Note>
        </Notes>
    </Examen>
    <Examen>
        <ExamenId>3</ExamenId>
        <Coef>3</Coef>
        <Notes>
            <Note>
                <EleveId>100</EleveId>
                <Valeur>6</Valeur>
            </Note>
            <Note>
                <EleveId>101</EleveId>
                <Valeur>5.4</Valeur>
            </Note>
            <Note>
                <EleveId>102</EleveId>
                <Valeur>2</Valeur>
            </Note>
        </Notes>
    </Examen>
</Examens>

The following snippet is correctly displaying the values (= ./Valeur) and the weight (= ./../../Coef) :

<xforms:repeat nodeset="$currentBranche//Note[EleveId=$currentEleveId]">
     <xforms:output ref="./Valeur"/>
     <xforms:output ref="./../../Coef"/>
</xforms:repeat>

BTW, I also need to exclude the nodes for which Valeur is an empty string. For example, in the following simple average calculation with the XPath avg() function, I got an error (“Cannot convert ” to double”) if one node’s content is an empty string. This is a problem, because the node exist (it’s part of a model instance) and the value is an empty string when the user has not yet entered a value.

<xforms:output ref="round(avg($currentBranche//Note[EleveId=$currentEleveId]/Valeur)*100) div 100"/>

[EDITED]

The correct calculations are :

If EleveId=100 : weighted average = (1*4+2*5+3*6) / (1+2+3) = 5.333
If EleveId=101 : weighted average = (1*4.2+3*5.4) / (1+3) = 5.1
If EleveId=102 : weighted average = (1*3.8+2*3.5+3*2) / (1+2+3) = 2.8

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:10:54+00:00Added an answer on June 16, 2026 at 10:10 pm

    In XPath 1.0 Use:

      sum($currentBranche//Note[EleveId=$currentEleveId]/Valeur[number(.)=number(.)])
    
     div
    
      count($currentBranche//Note[EleveId=$currentEleveId]/Valeur)
    

    In Xpath 2.0 (XQuery) use:

    round(avg($currentBranche//Note[EleveId=$currentEleveId]/Valeur
                                                 [number(.)=number(.)])*100
          ) div 100
    

    If all Valeur values are guaranteed to be castable as xs:decimal, then use:

    avg($currentBranche//Note[EleveId=$currentEleveId]/Valeur
                                                     [castable as xs:decimal]
                                                        /xs:decimal(.)
       )
    

    In this case there won’t be (noticeable) loss of precision and you can use later the format-number() function to get the wanted number of digits after the decimal point.


    II. Producing “weighted average”:

    Given the provided XML document:

    <Examens>
        <Examen>
            <ExamenId>1</ExamenId>
            <Coef>1</Coef>
            <Notes>
                <Note>
                    <EleveId>100</EleveId>
                    <Valeur>4</Valeur>
                </Note>
                <Note>
                    <EleveId>101</EleveId>
                    <Valeur>4.2</Valeur>
                </Note>
                <Note>
                    <EleveId>102</EleveId>
                    <Valeur>3.8</Valeur>
                </Note>
            </Notes>
        </Examen>
        <Examen>
            <ExamenId>2</ExamenId>
            <Coef>2</Coef>
            <Notes>
                <Note>
                    <EleveId>100</EleveId>
                    <Valeur>5</Valeur>
                </Note>
                <Note>
                    <EleveId>101</EleveId>
                    <Valeur/>
                </Note>
                <Note>
                    <EleveId>102</EleveId>
                    <Valeur>3.5</Valeur>
                </Note>
            </Notes>
        </Examen>
        <Examen>
            <ExamenId>3</ExamenId>
            <Coef>3</Coef>
            <Notes>
                <Note>
                    <EleveId>100</EleveId>
                    <Valeur>6</Valeur>
                </Note>
                <Note>
                    <EleveId>101</EleveId>
                    <Valeur>5.4</Valeur>
                </Note>
                <Note>
                    <EleveId>102</EleveId>
                    <Valeur>2</Valeur>
                </Note>
            </Notes>
        </Examen>
    </Examens>
    

    this XPath 2.0 expression produces the weighted average:

       for $elevId in distinct-values(/*/*/*/*/EleveId)
        return
          round(100*
                 (sum(/*/*/*/Note
                        [EleveId eq $elevId
                       and number(Valeur) eq number(Valeur)
                         ]
                          /(Valeur * ../../Coef)
                      )
                div
                  sum(/*/*/*/Note
                        [EleveId eq $elevId
                       and number(Valeur) eq number(Valeur)
                        ]
                         /../../Coef
                      )
                  )
               )
        div 100
    

    and the expected, correct result is produced:

    5.33 5.1 2.8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty simple task: using jquery I need to calculate the width of an input
I need to calculate the net total of a column-- sounds simple. The problem
I need to calculate the time till Christmas in milliseconds. I'm using joda time
I need to calculate the number of seconds that have passed between two events
I need to create a simple calculator that computes 1% of the value submitted
I need some simple calculations done using jquery. This example http://jsfiddle.net/tbL5r/ is pretty close.
I need a simple awk to calculate the sum of the number at the
I need to calculate the middle point (average) between two real numbers in JavaScript.
I've developed an equation parser using a simple stack algorithm that will handle binary
I need simple example for calculate PCB bit for APDU command. For example I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.