J
Jeremy Schneider
I am writing a script to validate some documents against an XSD. When a
document does not validate, I want to provide all the available
information about the reason(s) why it does not validate.
My problem is that it looks like two exceptions are raised in the
process of validating a document, but my method for dealing with the
exceptions only captures the last exception, and I would like to capture
all the exceptions.
Below is my program and its output. As you can see, two error messages
end up being sent to standard error, but the rescue part only captures
one (the last) error message.
Is there a good way to capture all of the exceptions generated in the
call to validate? I could play tricks like running the validation as a
system command and then parsing the standard error, but that seems very
clunky, and it feels like there should be a better way. I have tried
searching with Google and reading various references about exception
handling in Ruby, and not found the answer. Maybe I just don't know the
right terms to use.
Please forgive my naive Ruby code below. I'm just starting out,
obviously. Thanks for any assistance or guidance.
jeremy@jeremy-ubu810:~/xmlplay$ cat XML_Demo_5.rb
require 'rubygems'
require 'xml'
def myvalidate(theXMLDoc, theXSD)
valid = false
begin
theXMLDoc.validate(theXSD)
rescue StandardError => e
puts "message is: (#{e.to_s}); caller is: " + caller.inspect
else
valid = true
end
return valid
end
dtd = XML:td.new(%{<!ELEMENT rubycookbook (recipe+)>
<!ELEMENT recipe (title?, problem)+>
<!ELEMENT title (#PCDATA)>
<!ELEMENT problem (#PCDATA)> })
puts "Validation should fail:\n"
open('cookbook2.xml', 'w') do |f|
f.write %{<?xml version="1.0"?>
<rubycookbook>
<recipe>
<title>A recipe</title>
<title2>A recipe</title2>
</recipe>
</rubycookbook>
}
end
document = XML:ocument.file('cookbook2.xml')
if myvalidate(document,dtd)
puts "\nThe document was valid."
else
puts "\nThe document was not valid."
end
jeremy@jeremy-ubu810:~/xmlplay$ ruby XML_Demo_5.rb 2> stderr
Validation should fail:
message is: (Error: No declaration for element title2 at
cookbook2.xml:5.); caller is: ["XML_Demo_5.rb:36"]
The document was not valid.
jeremy@jeremy-ubu810:~/xmlplay$ cat stderr
Error: Element recipe content does not follow the DTD, expecting (title?
, problem)+, got (title title2 ) at cookbook2.xml:3.
Error: No declaration for element title2 at cookbook2.xml:5.
jeremy@jeremy-ubu810:~/xmlplay$
document does not validate, I want to provide all the available
information about the reason(s) why it does not validate.
My problem is that it looks like two exceptions are raised in the
process of validating a document, but my method for dealing with the
exceptions only captures the last exception, and I would like to capture
all the exceptions.
Below is my program and its output. As you can see, two error messages
end up being sent to standard error, but the rescue part only captures
one (the last) error message.
Is there a good way to capture all of the exceptions generated in the
call to validate? I could play tricks like running the validation as a
system command and then parsing the standard error, but that seems very
clunky, and it feels like there should be a better way. I have tried
searching with Google and reading various references about exception
handling in Ruby, and not found the answer. Maybe I just don't know the
right terms to use.
Please forgive my naive Ruby code below. I'm just starting out,
obviously. Thanks for any assistance or guidance.
jeremy@jeremy-ubu810:~/xmlplay$ cat XML_Demo_5.rb
require 'rubygems'
require 'xml'
def myvalidate(theXMLDoc, theXSD)
valid = false
begin
theXMLDoc.validate(theXSD)
rescue StandardError => e
puts "message is: (#{e.to_s}); caller is: " + caller.inspect
else
valid = true
end
return valid
end
dtd = XML:td.new(%{<!ELEMENT rubycookbook (recipe+)>
<!ELEMENT recipe (title?, problem)+>
<!ELEMENT title (#PCDATA)>
<!ELEMENT problem (#PCDATA)> })
puts "Validation should fail:\n"
open('cookbook2.xml', 'w') do |f|
f.write %{<?xml version="1.0"?>
<rubycookbook>
<recipe>
<title>A recipe</title>
<title2>A recipe</title2>
</recipe>
</rubycookbook>
}
end
document = XML:ocument.file('cookbook2.xml')
if myvalidate(document,dtd)
puts "\nThe document was valid."
else
puts "\nThe document was not valid."
end
jeremy@jeremy-ubu810:~/xmlplay$ ruby XML_Demo_5.rb 2> stderr
Validation should fail:
message is: (Error: No declaration for element title2 at
cookbook2.xml:5.); caller is: ["XML_Demo_5.rb:36"]
The document was not valid.
jeremy@jeremy-ubu810:~/xmlplay$ cat stderr
Error: Element recipe content does not follow the DTD, expecting (title?
, problem)+, got (title title2 ) at cookbook2.xml:3.
Error: No declaration for element title2 at cookbook2.xml:5.
jeremy@jeremy-ubu810:~/xmlplay$