from lxml import etree

xml = """\
<Foo xmlns:foo="http://example.com"
     xmlns="http://example.com">
Contents
</Foo>
"""

schema = """\
<xs:schema targetNamespace="http://example.com"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:foo="http://example.com">
    <xs:element name="Foo">
    </xs:element>
</xs:schema>
"""

schemaObj = etree.XMLSchema(etree=etree.fromstring(schema))
etree1 = etree.fromstring(xml)

etree1Out = etree.tostring(etree1, pretty_print=True)

nsmap={None: 'http://example.com', 'foo': 'http://example.com'}
rootElem = etree.Element('Foo', {}, nsmap)
rootElem.text = '\nContents\n'
etree2 = etree.ElementTree(element=rootElem)

etree2Out = etree.tostring(etree2, pretty_print=True)

if schemaObj.validate(etree1):
    print "etree1 is valid"
if schemaObj.validate(etree2):
    print "etree2 is valid"

if etree1Out == etree2Out:
    print "etree1Out and etree2Out are equal"
