# Assert XSD validity

**URL:** <https://www.ruby-forum.com/t/assert-xsd-validity/153622>\
**Category:** Rails\
**Created:** [November 19, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622 "2008-11-19T14:36:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [November 19, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622/1 "2008-11-19T14:36:56Z")

</div>

Hi.

I am working on a webservice producing xml content, In my rails  
functional test I would like to assert that this generated xml  
(@reponse.body) conforms to a specific XSD.

What is the best approach to do this?

Jarl

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [November 19, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622/2 "2008-11-19T15:40:23Z")

</div>

On 19 Nov 2008, at 13:07, Jarl F. wrote:

> Hi.
> 
> I am working on a webservice producing xml content, In my rails  
> functional test I would like to assert that this generated xml  
> (@reponse.body) conforms to a specific XSD.
> 
> What is the best approach to do this?

Dunno about best approach but I recently did something along those  
lines (in this particular case I was generating xml to send to a  
webservice rather than your way round, but I don’t think that’s  
relevant)  
in the end I just popened xmllint and piped my xml in. The ruby libxml  
bindings might allow you do stuff like that, I didn’t look very  
closely at that.

Fred

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [November 20, 2008, 11:57am UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622/3 "2008-11-20T11:57:31Z")

</div>

Thanks for the reply.

Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > 
> 
> Dunno about best approach but I recently did something along those  
> lines (in this particular case I was generating xml to send to a  
> webservice rather than your way round, but I don’t think that’s  
> relevant)  
> in the end I just popened xmllint and piped my xml in.

Cool, but xmllint only validates against a DTD, right? I am interested  
in (a more strict) validation against a XSD.

Jarl

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [November 20, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622/4 "2008-11-20T12:25:43Z")

</div>

On 20 Nov 2008, at 10:56, Jarl F. wrote:

> > > I am working on a webservice producing xml content, In my rails
> 
> Cool, but xmllint only validates against a DTD, right? I am interested  
> in (a more strict) validation against a XSD.

Does both as far as I know ( I only had an xsd anyway)

Fred

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [November 21, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622/5 "2008-11-21T12:21:12Z")

</div>

On 21 Nov 2008, at 11:13, Jarl F. wrote:

> 

stands for W3C XML schema apparently 🙂

Fred

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [November 21, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/assert-xsd-validity/153622/6 "2008-11-21T12:14:44Z")

</div>

Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Does both as far as I know ( I only had an xsd anyway)

Thanks. the xmllint help says  
–schema schema : do validation against the WXS schema

I have never seen “WXS” as an abreviation for XML Schema Definition  
files.

I now made a small rails test helper like this:

require ‘open3’  
class ActionController::TestCase  
def assert\_xsd\_validity(xsd, xml = @response.body )  
assert test(?e, xsd), “#{xsd} does not exist”  
xml\_temp\_file = Tempfile.new(“xsd\_helper\_tempfile”)  
xml\_temp\_file.write xml  
xml\_temp\_file.close  
command\_line = “/usr/bin/env xmllint -noout --schema #{xsd}  
#{xml\_temp\_file.path}”  
err = “”  
Open3.popen3(command\_line){|stdin, stdout, stderr|  
stdin.close  
out = stdout.read  
err = stderr.read  
}  
assert\_equal “#{xml\_temp\_file.path} validates\n”, err  
end  
end

Jarl
