import os
from lxml import etree

dirname = os.path.dirname(__file__)

def validate(cnxmlstr):
    schemafile = open(os.path.join(dirname,
                      'schema/cnxml/rng/0.7/cnxml.rng'))
    relaxng_doc = etree.parse(schemafile)
    relaxng = etree.RelaxNG(relaxng_doc)
    cnxmldoc= etree.fromstring(cnxmlstr)
    valid = relaxng.validate(cnxmldoc)
    if valid:
        print "Document validates against cnxml 0.7"
    else:
        print relaxng.error_log
    return valid, relaxng.error_log

def main():
    try:
        import argparse
        parser = argparse.ArgumentParser(description='Validate CNXML')
        parser.add_argument('cnxmlfile', help='/path/to/cnxmlfile', type=file)
        args = parser.parse_args()

        validate(args.cnxmlfile.read())
    except ImportError:
        print "argparse is needed for commandline"

if __name__ == '__main__':
    main()

@@ -3,16 +3,74 @@
 port os%0A
+import subprocess%0Afrom tempfile import NamedTemporaryFile%0A
 from lxm
@@ -143,16 +143,34 @@
 cnxmlstr
+, validator='lxml'
 ):%0A    s
@@ -179,19 +179,12 @@
 emaf
-ile
+n
  = 
-open(
 os.p
@@ -204,30 +204,8 @@
 ame,
-%0A                     
  'sc
@@ -234,18 +234,85 @@
 ml.rng')
-)
 %0A
+    if validator == 'lxml':%0A        schemafile = open(schemafn)%0A    
     rela
@@ -345,24 +345,28 @@
 mafile)%0A    
+   
+ 
 relaxng = et
@@ -386,24 +386,28 @@
 elaxng_doc)%0A
+    
     cnxmldoc
@@ -435,16 +435,20 @@
 xmlstr)%0A
+    
     vali
@@ -478,24 +478,28 @@
 xmldoc)%0A    
+ 
+   
 if valid:%0A  
@@ -496,16 +496,20 @@
  valid:%0A
+    
         
@@ -553,16 +553,20 @@
 ml 0.7%22%0A
+    
     else
@@ -571,24 +571,28 @@
 se:%0A        
+    
 print relaxn
@@ -603,16 +603,20 @@
 ror_log%0A
+    
     retu
@@ -642,16 +642,554 @@
 rror_log
+%0A    elif validator == 'jing':%0A        tmpfile = NamedTemporaryFile() # doesn't take stdin, so make a file%0A        tmpfile.write(cnxmlstr)%0A        tmpfile.flush()%0A        tmploc = tmpfile.name%0A        cmdargs = %5B%22jing%22, schemafn, tmploc%5D%0A        process = subprocess.Popen(cmdargs,%0A            stdin=subprocess.PIPE, stdout=subprocess.PIPE)%0A        stdout, stderr = process.communicate(cnxmlstr)%0A        valid = stdout is None%0A        return valid, stdout%0A    else:%0A        raise RuntimeError(%22Unknown validator: %25s%22 %25 validator)%0A        
 %0A%0Adef ma

add support for validating with jing