I have a collection of objects, and every time I want to put a new object in there, it would be nice to obtain an “empty” xml template of the object first, that matches the schema associated with it - then fill and store it. I am using the .NET API, and so far I create the xml I later want to store by hardcoding it (using a DOM and adding all elements and attributes manually) - that doesn’t look nice in my code.
I already thought about a solution, which would be having a separate collection with empty templates of all kind - or alternatively, using the collection where the object actually belongs to - adding an attribute to a single, empty xml object, e.g. isTemlate=“yes”.
I also retrieved the schema associated with the object, and tried to use it to generate a new xml based on the schema, but that is far too complicated for my needs.
Are there any other possibilities I have overlooked?
An alternative to hard coding is having such an empty document in an XML file that you read in and then modify in your program. If you do store an empty document in the database why not store it with a defined id (e.g. a GUID that you have generated).
Generating an empty document from a schema generally doesn’t make much sense as schema can potentially define and an infinite set of matching documents.
it seems to me that you want to have a guide for creating valid XML files from scratch, is that correct? Just as Mark Kuschnir already pointed out, this does not necessarily serve a general definition of a schema, which can for example be recursive, etc.
However, XML editors such as Oxygen allow you to associate an XML file to a schema and from then on provide context sensitive completion options. In fact some XML editors will provide you with an XML fragment with everything that is required, as soon as it it unique (for example if you insert
in an HTML file, it will add at least one li-element).
For a number of small documents I keep empty prototypes for XML files somewhere outside of Tamino (e.g. a very basic HTML file, etc.), because when querying for statistics, the pure existence of certain elements will change my results. I consider this as a hack, or as quick and dirty for creating XML documents to be valid.
That would be my recommendation…
Thanks for the answers!
Yes, I was searching for a possibility to create valid xml files from scratch. I understand that there are infinite possibilities to create valid xml files from a given schema, since there may exist optional elements and even references that cause recursion.
But then again, if there is a recursion defined within the schema, for example a person that has children that are also persons, they must always be optional. If they weren’t, there wouldn’t be any possibility to create an xml file that validates against that schema successfully, since the schema would expect an xml file with infinite depth - and that is quite impossible to create.
Anyway, I have to do some research as part of a diploma thesis - which focuses on the aspect of doing such things in a more generic way. If I understood everything right, I have to traverse the schema recursively, check for such things like if the elements are optional, or if they are referencing other elements or datatypes that may also be optional and further may contain other optional elements, sequences and so on… and create the xml - or maybe other data that is used to create or modify the xml-data - on that basis. So I’d be able to build at least the most minimal xml that validates against the Schema by ignoring all optional elements.
Altova’s xmlspy has such a built in schema-parser for examle, but that’s no use since I want to keep the things utmost dynamic.
I am using the .NET API and I’m experiencing difficulties traversing the Schema Object Model using the XmlSchema-class. Does anybody have experience doing so?