Re: [xml] default values from schema



On Sat, Mar 26, 2016 at 09:48:59PM +0100, Fabian Wein wrote:
Hello to the list,

I know I have a FAQ but I cannot manage it by myself :(

I have a xsd schema and need to validate *and* parse a xml file making use
of the default values from the schema. 

In the end I need it in C++ but I guess it helps to have it first in Python?!

My minimalistic example is based on http://www.w3schools.com/xml/schema_howto.asp
extended by attributes, one with default -> see attachment.

Validating works nicely :) Any error in the xml is detected or ret=0 if the file is ok.

doc = libxml2.parseFile(„node.xml“)
s = libxml2.schemaNewParserCtxt(„node.xsd“)
sp = s.schemaParse()
ct = sp.schemaNewValidCtxt()  
ret = ct.schemaValidateDoc(doc)

But I have no idea on how to get the populated data from this (only from doc).

Then I tried to extend the reader example:

def streamFile(filename):
    reader = libxml2.newTextReaderFilename(filename)
    s = libxml2.schemaNewParserCtxt(‚node.xsd‘)
    sp = s.schemaParse()
    ct = sp.schemaNewValidCtxt()
    ct.schemaSetValidOptions(libxml2.XML_SCHEMA_VAL_VC_I_CREATE)
    ct.SchemaValidateCtxt(reader, libxml2.XML_SCHEMA_VAL_VC_I_CREATE)
    
    reader.SetSchema(ct)
    reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS, 1)
    ret = reader.Read()
    while ret == 1:
        processNode(reader)
        ret = reader.Read()

However, with the reader api the root element is not recognized and the default attribute is not set:

Element ‚{http://www.w3schools.com}note‘: No matching global declaration available for the validation root.

As the schema is validated properly in the first code I believe the schema is ok.

So my questions are:
* Which api shall I use to make use of the default values?

  I don't think the toolkit has the support in XML schemas to populate default
values back in the tree.

* Shall I try in C instead of Python?
* Is there a link which helps me? I spent two days googling without success :(

  and that explain why,

Thanks a lot!

   sorry,

Daniel
Fabian










_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
https://mail.gnome.org/mailman/listinfo/xml


-- 
Daniel Veillard      | Open Source and Standards, Red Hat
veillard redhat com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | virtualization library  http://libvirt.org/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]