0

I am using libxml2 python for parsing my xml and validate it against an xsd file.

I was unable to find out the line number of the error in xml file when i get an schema validation error.

Is there a way?

For better understanding i have pasted my code below:

ctxt = libxml2.schemaNewParserCtxt("xsd1.xsd")

schema = ctxt.schemaParse()

del ctxt

validationCtxt = schema.schemaNewValidCtxt()

doc = libxml2.parseFile("myxml.xml")
iErrA1 = validationCtxt.schemaValidateDoc(doc)

#get the line number if there is error

Update:

I tried libxml2.lineNumbersDefault(1), this probably enables the library to collect line numbers. Then i register a callback through validationCtxt.setValidityErrorHandler(ErrorHandler, ErrorHandler). I get the msg part but no line number is present. More info: I had seen a line number member in xmlError, but its unclear to me as to how I will get this xmlError type object back when there is an error. Also there is a global function lastError which returns an xmlError type object, but it always returns 'none' although there is an error

nitsi
  • 1
  • 3
  • see this link : http://stackoverflow.com/questions/6949395/is-there-a-way-to-get-a-line-number-from-an-elementtree-element – Sameer H. Ibra May 15 '13 at 09:47
  • Thanks Sameer, but i have a constraint of using libxml2, I need to find it and report it to another tool – nitsi May 15 '13 at 10:21
  • http://dsnra.jpl.nasa.gov/software/Python/site-packages/Support/libxml2.html : have documnetation for this library and handle the error – Sameer H. Ibra May 15 '13 at 10:27
  • nope, i had gone through the documentation before.. but didnot understand how to get the line number – nitsi May 15 '13 at 11:39
  • 1
    did you try using this one? [link](http://dsnra.jpl.nasa.gov/software/Python/site-packages/Support/libxml2.html#SchemaValidCtxtCore) `setValidityErrorHandler(self, err_func, warn_func, arg=None) Register error and warning handlers for Schema validation. These will be called back as f(msg,arg)` – Krishna May 15 '13 at 14:05
  • yes i had tried it before.. i used first libxml2.lineNumbersDefault(1), this probably enables the library to collect line numbers. Then i had registered a error handling function. I get the msg part but no line number is present. Also i had seen a line number member in xmlError, but i didnot understand how to get an xmlError type object back when there is an error. There is a global function lastError which returns an xmlError type object, but it always returns 'none' although there is an error – nitsi May 16 '13 at 00:33

0 Answers0