Getting error information from XML Schema validation

Hi

I am using libxml2 to validate an XML file using a Schema. Like this:

schema = XML::Schema.from_string(schema_string)
parser = XML::Parser.new
parser.string = xml
doc = parser.parse
result = doc.validate_schema(schema)

This works and if the XML doc is invalid the error is written to
standard output. Is there a way to capture this output? I need to
relay this information to the client application. Grateful for any
assistance.

Kindest regards

Erik

I found the solution in the libxml-ruby test cases. Viva TDD! If
anyone else is interested this is a pattern for testing it:

schema = XML::Schema.from_string(schema_string)
parser = XML::Parser.new
parser.string = xml
doc = parser.parse

messages = Hash.new
assert(!doc.validate_schema(schema) do |message, error|
  messages[message] = error
end)

expected = {"Element 'customer_ip': This element is not expected.

Expected is ( password ).\n" => true}
assert_equal expected, messages

Hope it helps.

Regards

Erik

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs