A cannot see my answer on the libxml2 mailing list, I send it
again.

Daniel Veillard wrote:
> 
>   Not really. Can you provide the smallest XML files for the
> RNG and the XML showing the problem, then we can debug what's actually
> happening.
> 
See attachment.
Actually, the error is catched, but the returned diag is not usable.
I wonder if I have to use the lib in an other way in order to get a
usable error message ?

-MP-
<DimensionalUnits_t 
  Name="DimensionalUnits"  
  LengthUnits="Meter"  
  TimeUnits="Second"
/>

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"; 
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes";>
  <define name="DimensionalUnits_t">
    <element name="DimensionalUnits_t">
      <attribute name="Name">
        <value type="string">DimensionalUnits</value>
      </attribute>
      <interleave>
        <attribute name="LengthUnits">
          <ref name="LengthUnits_t"/>
        </attribute>
        <attribute name="TimeUnits">
          <ref name="TimeUnits_t"/>
        </attribute>
      </interleave>
    </element>
  </define>
  <define name="LengthUnits_t">
    <choice>
      <value>Meter</value>
      <value>Centimeter</value>
      <value>Millimeter</value>
    </choice>
  </define>
  <define name="TimeUnits_t">
    <choice>
      <value>Second</value>
      <value>UserDefined</value>
    </choice>
  </define>
  <start>
    <ref name="DimensionalUnits_t" />
  </start>
</grammar>

#!/usr/bin/env python
# TESTED with v2.6.17 on SGI IRIX 6.5 (native compiler mode -64)
# Python 2.3.4

# [1] Test it without chage -> ok
# [2] Change an enumnerate value to invalid (e.g. in enumData.xml
#     set "Meters" instead of "Meter" -> error code cannot give any info
#     on line, attribute name, etc...
#     -> very difficult to handle when you have thousand of xml lines !

import libxml2

schema_name="enumGrammar.rng"
data_name="enumData.xml"

# schema
inputs = open(schema_name, 'r')
schema = inputs.read()
inputs.close()

# XML target file
inputd = open(data_name, 'r')
data = inputd.read()
inputd.close()

# error handler
def myParseError(localcontext,unused):
  e = libxml2.lastError()
  print "file :", e.file()
  print "level:", e.level()
  print "line :", e.line()
  print "msg  :", e.message()

# rng parser
libxml2.registerErrorHandler(myParseError,"%s Schema Syntax"%schema_name)
rngp = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
rngs = rngp.relaxNGParse()

# xml doc
ctxt = rngs.relaxNGNewValidCtxt()
doc  = libxml2.parseDoc(data)
ret  = doc.relaxNGValidateDoc(ctxt)

# last line

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to