Hi there,
I'm in the process of testing hooks for reporting libxml2's so-called
"structured errors" in lxml. Among other things, I want to get a more
diagnostic message when a document fails to validate against a RelaxNG
schema.
I ran across a problem where an invalid document will fail the
validation the first time but succeed when the validation is repeated. I
haven't been able to isolate a small test case, unfortunately; my
attempts at simplification give the expected results.
I am on OS X 10.4 using lxml pristine from SVN, libxml2 2.6.20 and
libxslt 1.1.14 (note: these are *not* the ones that come with the OS but
the problem exists there, too).
Attached are the RelaxNG schema, the invalid document, and a failing
unit test. I hope they're not too large for the list. I apologize in
advance if they are. FYI: the invalid element is
/notebook/sheet/ipython-block/para on line 18.
Thank you, in advance!
--
Robert Kern
rkern(a)ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
<notebook>
<head>
<meta content="/usr/local/bin/ipython" name="cmdline">
</meta>
</head>
<sheet>
<title>Scipy Tutorial</title>
<para>There are two (interchangeable) ways to deal with 1-d polynomials in Scipy. The
first is to use the <emphasis role="bold">poly1d</emphasis> class in
<emphasis role="bold">scipy_base</emphasis>. This class accepts
coefficients or polynomial roots to initialize a polynomial. The polynomial
object can then be manipulated in algebraic expressions, integrated,
differentiated, and evaluated. It even prints like a polynomial:
</para>
<ipython-block logid="default-log">
<para>This shouldn't be here.</para>
<ipython-cell type="input" number="3">
</ipython-cell>
<ipython-cell type="input" number="4">
</ipython-cell>
<ipython-cell type="input" number="5">
</ipython-cell>
<ipython-cell type="stdout" number="5">
</ipython-cell>
<ipython-cell type="input" number="6">
</ipython-cell>
<ipython-cell type="stdout" number="6">
</ipython-cell>
<ipython-cell type="input" number="7">
</ipython-cell>
<ipython-cell type="stdout" number="7">
</ipython-cell>
<ipython-cell type="input" number="8">
</ipython-cell>
<ipython-cell type="output" number="8">
</ipython-cell>
<ipython-cell type="input" number="9">
</ipython-cell>
<ipython-cell type="output" number="9">
</ipython-cell>
</ipython-block>
<para>The other way to handle polynomials is an array of coefficients with the first
element of the array giving the coefficient of the highest power. There are
explicit functions to add, subtract, multiply, divide, integrate, differentiate,
and evalute polynomials represented as sequences of coefficients.
</para>
</sheet>
<ipython-log id="default-log"><cell number="3"><input>
from scipy import *
</input></cell>
<cell number="4"><input>
p = poly1d([3,4,5])
</input></cell>
<cell number="5"><input>
print p
</input><stdout> 2
3 x + 4 x + 5
</stdout></cell>
<cell number="6"><input>
print p*p
</input><stdout> 4 3 2
9 x + 24 x + 46 x + 40 x + 25
</stdout></cell>
<cell number="7"><input>
print p.integ(k=6)
</input><stdout> 3 2
x + 2 x + 5 x + 6
</stdout></cell>
<cell number="8"><input>
p.deriv()
</input><output>poly1d([6, 4])
</output></cell>
<cell number="9"><input>
p([4,5])
</input><output>array([ 69, 100])
</output></cell>
</ipython-log>
</notebook>
import unittest
from lxml import etree
class DuplicateRelaxNGTestCase(unittest.TestCase):
def test_once(self):
rng = etree.RelaxNG(file='nbk.rng')
baddoc = etree.parse('tut-2.3.5-bad.nbk')
self.assert_(not rng.validate(baddoc))
# try again
self.assert_(not rng.validate(baddoc))
def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(DuplicateRelaxNGTestCase)])
return suite
if __name__ == '__main__':
unittest.main()