Class: DS::Manifest::SimpleXmlIdValidator

Inherits:
BaseIdValidator show all
Defined in:
lib/ds/manifest/simple_xml_id_validator.rb

Instance Attribute Summary collapse

Attributes inherited from BaseIdValidator

#errors, #source

Instance Method Summary collapse

Methods inherited from BaseIdValidator

#add_error, #handle_count_error, #valid?

Constructor Details

#initialize(source, namespaces = {}) ⇒ SimpleXmlIdValidator

Returns a new instance of SimpleXmlIdValidator.



9
10
11
12
# File 'lib/ds/manifest/simple_xml_id_validator.rb', line 9

def initialize source, namespaces = {}
  @namespaces = namespaces.present? ? namespaces : DS::Constants::XML_NAMESPACES
  super source
end

Instance Attribute Details

#namespaces ⇒ Object

Returns the value of attribute namespaces.



7
8
9
# File 'lib/ds/manifest/simple_xml_id_validator.rb', line 7

def namespaces
  @namespaces
end

Instance Method Details

#locate_record(source_path, id, id_location) ⇒ Nokogiri::XML::NodeSet

Locates a record in the XML document based on the given source path, ID, and ID location.

id_location should be a template XPath expression that returns one or more records, for example:

"//record[controlfield[@tag='001'] = 'ID_PLACEHOLDER']"

The string 'ID_PLACEHOLDER' must be in the template.It will be replaced with the ID of the record to locate.

Parameters:

  • source_path (String) —

    the path to the XML source file

  • id (String) —

    the ID of the record to locate

  • id_location (String) —

    the XPath expression to locate the record

Returns:

  • (Nokogiri::XML::NodeSet) —

    the located record(s)



28
29
30
31
32
# File 'lib/ds/manifest/simple_xml_id_validator.rb', line 28

def locate_record source_path, id, id_location
  locator = DS::Extractor::XmlRecordLocator.new namespaces: namespaces
  xml = source.load_source source_path
  locator.locate_record xml, id, id_location
end

#try_locate_record(xml, xpath, namespaces: nil) ⇒ Object



34
35
36
37
38
39
# File 'lib/ds/manifest/simple_xml_id_validator.rb', line 34

def try_locate_record xml, xpath, namespaces: nil
  xml.xpath xpath, namespaces
rescue Nokogiri::XML::XPath::SyntaxError => e
  raise unless e.message =~ /undefined namespace prefix/i
  []
end