Re: [xml] Create a Schema instance from a W3C Schema



On 15 Oct 12:07, Henrik Frisk wrote:
}Is it possible to generate an (empty) instance document from an XML (W3C)
}Schema using libxml2? I've been searching the docs and the lists on this
}topic but please let me know if I've missed something (I'm a newbie!).

As I was told when I asked the same question, no, not as part of libxml2.
In spite of the apparent simplicity of xml, once you start using schemas
things get very ugly, and it's not a straight forward process to map xsd
to xml.

The classic way to do it in java is with something like xmlbeans which
generates some code with get/set methods. If your schema changes you
regenerate and recompile your code.

There are some commercial tools which do what you want and they may be
worth investigating.

I wrote my own solution which I required for a project. It's in what I
would call beta stage. Basically it works pretty well for the bits of
the schema definition that were used by the schema I am working with,
but there are other bits unimplemented.

It did require some internals from xmlschemas.c and xmltree.c so it would
be best considered as part of libxml. I also assume you are starting with
a valid schema (as there are separate means to verify that).

My method also generates a template document from the schema at run
time by using a callback, then uses another callback to fill in data
values from the template to generate an xml instance document. There is
therefore a slight performance penalty over the precompiled method, but
it's possible to rapidly generate new applications using the API by
defining a callback which just looks up a mapping table of xpath to your
app's data structure (which is what I did).

I've been working with modestly sized xml documents of around 4-5k with
about 130 elements and it's working more than fast enough for my needs.

I'd be keen on contributing it back to the community, and even having
it incorporated into the main library if it was considered worthy, and
so that support for other schema elements could be added.

The only consideration is that I probably need to get some clearance
from my work to release the code. Hopefully there shouldn't be any problem
there since there is no business-related intellectual property in what
I've done - it's a generic solution.

    C

-- 

Callum Gibson @ home




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