example_files/resources/hpxml-measures/HPXMLtoOpenStudio/resources/xmlvalidator.rb in urbanopt-cli-0.10.0 vs example_files/resources/hpxml-measures/HPXMLtoOpenStudio/resources/xmlvalidator.rb in urbanopt-cli-0.11.0

- old
+ new

@@ -1,5 +1,10 @@ +# ********************************************************************************* +# URBANopt (tm), Copyright (c) Alliance for Sustainable Energy, LLC. +# See also https://github.com/urbanopt/urbanopt-cli/blob/develop/LICENSE.md +# ********************************************************************************* + # frozen_string_literal: true class XMLValidator def self.get_schema_validator(schema_path) return OpenStudio::XMLValidator.new(schema_path) @@ -53,27 +58,39 @@ # Error msg_txt = XMLHelper.get_value(n, 'svrl:text', :string) # Try to retrieve SystemIdentifier context_element = hpxml_doc.xpath(current_context.gsub('h:', ''))[current_context_idx] - sys_id = XMLHelper.get_attribute_value(XMLHelper.get_element(context_element, 'SystemIdentifier'), 'id') - if sys_id.nil? + if context_element.nil? + fail "Could not find element at xpath '#{current_context}' with index #{current_context_idx}." + end + + element_id = get_element_id(context_element) + if element_id.nil? # Keep checking parent elements context_element.each_ancestor do |parent_element| - sys_id = XMLHelper.get_attribute_value(XMLHelper.get_element(parent_element, 'SystemIdentifier'), 'id') - break unless sys_id.nil? + element_id = get_element_id(parent_element) + break unless element_id.nil? end end - sys_id_string = ", id: \"#{sys_id}\"" unless sys_id.nil? + element_id_string = ", id: \"#{element_id}\"" unless element_id.nil? - full_msg = "#{msg_txt} [context: #{current_context.gsub('h:', '')}#{sys_id_string}]" + full_msg = "#{msg_txt} [context: #{current_context.gsub('h:', '')}#{element_id_string}]" if n.name == 'failed-assert' errors.append(full_msg) elsif n.name == 'successful-report' warnings.append(full_msg) end end end end return errors, warnings + end + + def self.get_element_id(element) + if element.name.to_s == 'Building' + return XMLHelper.get_attribute_value(XMLHelper.get_element(element, 'BuildingID'), 'id') + else + return XMLHelper.get_attribute_value(XMLHelper.get_element(element, 'SystemIdentifier'), 'id') + end end end