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 4593932
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:45:24+00:00 2026-05-21T22:45:24+00:00

I have a question about formatting the output of the sum() function in XQuery/XSLT/XPath.

  • 0

I have a question about formatting the output of the sum() function in XQuery/XSLT/XPath. I have both an XSLT stylesheet and also an XQuery. I’m able to get the correct formatting in the XSLT, but not in the XQuery.

Here is an example input file:

<userTotals>
  <user cost="138764.63" hours="1506.51"/>
  <user cost="329555.76" hours="3577.85"/>
  <user cost="213909.81" hours="2322.33"/>
  <user cost="22684.85" hours="246.28"/>
  <user cost="6147.42" hours="66.74"/>
  <user cost="1269.27" hours="13.78"/>
  <user cost="181.45" hours="1.97"/>
  <user cost="755.30" hours="8.2"/>
</userTotals>

Here is an example stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/userTotals">
    <results>
      <cost><xsl:value-of select="sum(user/@cost)"/></cost>
      <hours><xsl:value-of select="sum(user/@hours)"/></hours>
      <hours-formatted>
        <xsl:value-of select="format-number(sum(user/@hours),'###.##')"/>
      </hours-formatted>
    </results>
  </xsl:template>

</xsl:stylesheet>

Here is the output:

<results>
   <cost>713268.49</cost>
   <hours>7743.659999999999</hours>
   <hours-formatted>7743.66</hours-formatted>
</results>

Notice the output of <hours> and <hours-formatted>. The content of <hours> is the unmodified/unformatted output of sum(). The content of <hours-formatted> is the way I want the number formatted in both the XSLT and the XQuery.

The problem is that in XQuery, format-number() is not available. How can I format the results of sum() the same way in both XSLT and XQuery so that the results will appear like <hours-formatted>?

Will I need to create a function that takes the output of sum() as a string and process the decimal myself? In this case I would need to round .659 to .66. I’m hoping there is a better way to do this (maybe cast the results as a different type and then ??).

Thank you for any info/guidance you can provide.

  • 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-05-21T22:45:24+00:00Added an answer on May 21, 2026 at 10:45 pm

    Saxon-PE and upwards offers format-number() in XQuery as an extension function (or by enabling XQuery 3.0 support).

    But you can probably achieve what you want using the round-half-to-even() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently asked a question about formatting JavaScript code in Vim. And I've also
i have question about why should we use return in get,if dont use what
I have a question about formatting the Rupee currency (Indian Rupee - INR). Typically
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,

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.