5. Using SaxonJS on Node

On Node.js, you must explicitly import the SaxonJS object with require. Then you can use any of it’s APIs directly from JavaScript.

The following Node.js program uses XPath to calculate the number of days until Christmas.

'use strict';
const SaxonJS = require('saxon-js');
const DECEMBER = 11; // 0-based counting!
 
const today = new Date();
let year = today.getFullYear();
if (today.getMonth() == DECEMBER
    && today.getDate() >= 25) {
  year += 1;
}
 
let christmas = `${year}-12-25`;
let options = { "params": {"Q{}christmas": christmas} };
 
let days = SaxonJS.XPath.evaluate(
  `let $duration := xs:date($christmas) - current-date()
   return
     if ($duration <= xs:dayTimeDuration("P1D"))
     then 'Christmas is TOMORROW!'
     else 'It''s '
          || days-from-duration($duration)
          || ' days ''til Christmas'`,
  null, options);
 
console.log(days);