# Getting error information from XML Schema validation

**URL:** <https://www.ruby-forum.com/t/getting-error-information-from-xml-schema-validation/146987>\
**Category:** Rails\
**Created:** [August 27, 2008, 10:37am UTC](https://www.ruby-forum.com/t/getting-error-information-from-xml-schema-validation/146987 "2008-08-27T10:37:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [August 27, 2008, 10:37am UTC](https://www.ruby-forum.com/t/getting-error-information-from-xml-schema-validation/146987/1 "2008-08-27T10:37:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![makire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@makire](https://www.ruby-forum.com/u/makire)\
**Post date:** [August 27, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/getting-error-information-from-xml-schema-validation/146987/2 "2008-08-27T12:00:16Z")

</div>

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
