Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Parse XML into INT to sum numbers up

I have an XML that I'm trying to get a sum out of. The number is either 0 or 1, and I want to get a sum of all the numbers. The XML looks like this:

<alerts>
    <alert1>0</alert1>
    <alert2>1</alert2>
    <alert3>1</alert3>
    <alert4>0</alert4>
</alerts>

My current code (wrong) looks like this:

xpath = require('xpath');
xmldom = require('xmldom');

doc = new xmldom.DOMParser().parseFromString("sourceXml");

var count = xpath.select("alerts/alert1", doc).toString();
count += xpath.select("alerts/alert2", doc).toString();
count += xpath.select("alerts/alert3", doc).toString();
count += xpath.select("alerts/alert4", doc).toString();


var result = count;

Obviously since they're strings it results in "0110" as a string and not "2" as an INT. I tried wrapping each in parseInt but that just returns NaN. Any help would be appreciated.

Edit:

parseInt(xpath.select("alerts/alert3", doc).toString());

parseInt(xpath.select("alerts/alert3", doc)); 

Tried both but I'm not very experienced with JavaScript so it could be a syntax issue.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

From the xpath.select() documentation...

The return value is determined based on the result type of the expression (which can always be predicted ahead of time based on the expression's syntax):

  • A boolean value if the expression evaluates to a boolean value.
  • A number if the expression evaluates to a numeric value.
  • A string if the expression evaluates to a string.
  • If the expression evaluates to a nodeset:
    • An array of 0 or more nodes if single is unspecified or falsy
    • A single node (the first node in document order) or undefined if single is truthy

All your results are arrays, each containing a single node.

You can actually coerce the values to be numeric automatically by using the XPath number function

const count = xpath.select("number(alerts/alert1)", doc)
 + xpath.select("number(alerts/alert2)", doc)
 + xpath.select("number(alerts/alert3)", doc)
 + xpath.select("number(alerts/alert4)", doc)

Perhaps an easier option would be to simply use the XPath sum function

const count = xpath.select(
  "sum(alerts/*[self::alert1 or self::alert2 or self::alert3 or self::alert4])",
  doc
)

See also XPath to select multiple tags

about 4 years ago · Juan Pablo Isaza Report

0

What's wrong with this:

const count = xpath.select("sum(alerts/*)", doc) 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!