<commit_before>/*
 *
 */

/*
 * オブジェクト構造
 * {
 *   summarydata: {
 *     beginning: , 
 *     duration: ,
 *     durationstopped: , 
 *     work: , 
 *     hr: { max:, min:, avg: }, 
 *     spd: { max:, min:, avg: }, 
 *     dist: , 
 *     alt: { max:, min:, avg: }, 
 *   }, 
 *   sample: [{
 *     timeoffset:, 
 *     spd:, 
 *     cad:, 
 *     dist:, 
 *     lat:, 
 *     lon:, 
 *     alt:, 
 *   }],
 * }
 */

var pwx = {
  summarydata: {}, 
  sample: [], 

  /*
   * xml: DOMParser.parseFromString(xmltext, 'text/html')　しておいて下さい
   */
  readFromXML: function(xml) {
    var obj = {};
    var summaryData = {};
    var elem;
    var result = null;

    var num = function(name) {
      result = xml.evaluate('/pwx/workout/summarydata/' + name, xml, null, XPathResult.FIRST_ORDERED_NODE_TYPE, result);
      console.log(result);
      console.log(result.resultType);
      if(result) { 
	var elem = result.singleNodeValue;
	summaryData[name] = elem;
	console.log(">>> " + elem);
      }
      else { console.log(name + " not found"); }
    };
    var mma = function(name) {
      var mma = {};
      var found = false;

      result = xml.evaluate('/pwx/workout/summarydata/' + name + '/@max', xml, null, XPathResult.STRING_TYPE, result);
      if(result) { mma['max'] = result.stringValue; found = true; }
      else { console.log(name + " max not found"); }

      result = xml.evaluate('/pwx/workout/summarydata/' + name + '/@min', xml, null, XPathResult.STRING_TYPE, result);
      if(result) { mma['min'] = result.stringValue; found = true; }
      else { console.log(name + " min not found"); }

      result = xml.evaluate('/pwx/workout/summarydata/' + name + '/@avg', xml, null, XPathResult.STRING_TYPE, result);
      if(result) { mma['avg'] = result.stringValue; found = true; }
      else { console.log(name + " avg not found"); }

      if(found) {
	summaryData[name] = mma;
      }
    }

    num('beginning');
    num('duration');
    num('durationstopped');
    num('work');
    mma('hr');
    mma('spd');
    mma('cad');
    num('dist');
    mma('alt');
    obj.summarydata = summaryData;

    return obj;
  }, 

  checkCompat: function() {
    return window.DOMParser;
  }, 
};

/* debug code */
function debugmorpwx() {
  console.log("launch morpwx debug");
  var domParser = new DOMParser();
  var elem = document.getElementById('xmlarea');
  var xml = domParser.parseFromString(elem.value, 'text/xml');
  var obj = pwx.readFromXML(xml);
  console.log("result as follows");
  console.log(obj.summarydata.duration);
  console.log(obj.summarydata.work);
  return false;
};
<commit_msg>parser committed but namespace doesn't work<commit_after>/*
 *
 */

/*
 * オブジェクト構造
 * {
 *   summarydata: {
 *     beginning: , 
 *     duration: ,
 *     durationstopped: , 
 *     work: , 
 *     hr: { max:, min:, avg: }, 
 *     spd: { max:, min:, avg: }, 
 *     dist: , 
 *     alt: { max:, min:, avg: }, 
 *   }, 
 *   sample: [{
 *     timeoffset:, 
 *     spd:, 
 *     cad:, 
 *     dist:, 
 *     lat:, 
 *     lon:, 
 *     alt:, 
 *   }],
 * }
 */

var pwx = {
  summarydata: {}, 
  sample: [], 

  /*
   * xml: DOMParser.parseFromString(xmltext, 'text/html')　しておいて下さい
   */
  readFromXML: function(xml) {
    var obj = {};
    var summaryData = {};
    var result = null;

    var nsResolver = { lookupNamespaceURI : function(prefix) {
      console.log("prefix " + prefix);
      return "http://www.peaksware.com/PWX/1/0";
    }};

    var num = function(name) {
      result = xml.evaluate('/pwx/workout/summarydata/' + name, xml, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, result);
      var elem = result.singleNodeValue;
      if(elem) { 
	summaryData[name] = elem.textContent;
	console.log(">>> " + name + " " + summaryData[name]);
      }
    };
    var mma = function(name) {
      var prop = {};
      var found = false;

      result = xml.evaluate('/pwx/workout/summarydata/' + name, xml, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, result);
      var elem = result.singleNodeValue;
      if(elem) { 
	prop.max = elem.getAttributeNode('max').textContent;
	prop.min = elem.getAttributeNode('min').textContent;
	prop.avg = elem.getAttributeNode('avg').textContent;
	summaryData[name] = prop;
	console.log(">>> " + name + " max:" + prop.max + " min:" + prop.min + " avg:" + prop.avg);
      }
    }

    num('beginning');
    num('duration');
    num('durationstopped');
    num('work');
    mma('hr');
    mma('spd');
    mma('cad');
    num('dist');
    mma('alt');
    obj.summarydata = summaryData;

    result = null;
    return obj;
  }, 

  checkCompat: function() {
    return window.DOMParser;
  }, 
};

/* debug code */
function debugmorpwx() {
  console.log("launch morpwx debug");
  var domParser = new DOMParser();
  var elem = document.getElementById('xmlarea');
  var xml = domParser.parseFromString(elem.value, 'text/xml');
  var obj = pwx.readFromXML(xml);
  console.log("result as follows");
  console.log(obj.summarydata.duration);
  console.log(obj.summarydata.work);
  return false;
};

{'fraction_non_alphanumeric': 0.1780550604080016, 'fraction_numerical': 0.0003961180431768667, 'mean_word_length': 2.6045681655960027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6732840', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1752, 'n_words': 340}