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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:45:14+00:00 2026-06-15T23:45:14+00:00

Ok so lets say i have an xml what looks like this example, <?xml

  • 0

Ok so lets say i have an xml what looks like this example,

   <?xml version="1.0" encoding="UTF-8"?>

   <stats id="865" name="blaah">
       <example name="blaahblaah">
           <example1>
               <x ppoints="500"/>
           </example1>
           <example2>
               <x ppoints ="150"/>
               <x points ="500"/>
               <x ppoints ="140"/>
               <x points="200"/>
           </example2>
       </example>
   </stats>

what i am trying to do here is to get all the ppoints and round them into one number and all the points and round them into one number,
like so

Stats: “rounded up ppoints” pp & “rounded up points” p

help anyone? many thanks

  • 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-15T23:45:15+00:00Added an answer on June 15, 2026 at 11:45 pm

    In this case, as I understand the structure of the document can contain multiple ‘example’ nodes, I think the best approach is to load the XML document and use a XPath expression to get all the nodes with a ppoints attribute and a points attribute.

    Using XPath there are two possible solutions. One using SimpleXMLElement::xpath to retrieve all the required nodes and sum them manually. And another one using DOMXPath::evaluate which allows to evaluate any type of XPath expression and sum the values just using XPath. The latter solutions is simpler.

    DOMXPath::evaluate

    <?php
    
    $doc = new DOMDocument;
    $doc->load('file.xml');
    $xpath = new DOMXPath($doc);
    
    $sum_ppoints = $xpath->evaluate('sum(//x/@ppoints)');
    $sum_points = $xpath->evaluate('sum(//x/@points)');
    
    print "Ppoints: $sum_ppoints; Points: $sum_points\n";
    
    ?>
    

    SimpleXMLElement::xpath

    <?php
    
    // Load XML file
    $file = "file.xml";
    $content = file_get_contents($file);
    $xml = new SimpleXMLElement($content);
    
    // Compute sum ppoints
    $sum_ppoints = 0;
    $nodes = $xml->xpath('//x[@ppoints]');
    foreach ($nodes as $node) {
        $sum_ppoints += $node->attributes()->ppoints;
    }
    
    // Compute sum points
    $sum_points = 0;
    $nodes = $xml->xpath('//x[@points]');
    foreach ($nodes as $node) {
        $sum_points += $node->attributes()->points;
    }
    
    print "Ppoints: $sum_ppoints; Points: $sum_points\n";
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a xslt stylesheet like the following: <?xml version=1.0 encoding=utf-8?> <xsl:stylesheet
So i have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <gesmes:Envelope
Let's just say I have an XML file that looks like this: <?xml version=1.0
Let's say that I have something like this in my web.xml file. <filter name=foo>
Let's say I have an XML file that looks like this: <a id=x> <b>
Lets say I have XML that looks like so (and assume I can't alter
lets say i have an xml file path.xml that goes like this: <paths> <path
Lets say I have the following xml (a quick example) <rows> <row> <name>one</name> </row>
Let's say I have a file-system that looks a little something like this: C:\stuff\build.xml
Lets say I have a custom data type that looks something like this: public

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.