is it somehow possible to generate an empty object from an XML schema with JSONIX? I generally have problems creating new JS objects that fit to the XML schema. Thus, this would be really helpful. Any example would be very appreciated. I tried the following to create a new object. NodeType is a complex type name in this case.
{name: {localpart: nodeType}, value:{}};
Then I tried to fill values (I traverse through the schema mappings to find out all possible properties for each type). However, I get e.g. the following error that does not help me very much: Element [ELEMNAME] is not known in this context
If this is not possible, how do I in general create a new object that is supposed to be conform to the schema?
Thanks a lot for any idea!
EDIT: Okay to be more specific here an example:
"NodeType":{
"type":"object",
"title":"NodeType",
"properties":{
"id":{
"title":"id",
"allOf":[
{
"$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
}
],
"propertyType":"attribute",
"attributeName":{
"localPart":"id",
"namespaceURI":""
}
},
"x":{
"title":"x",
"allOf":[
{
"$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
}
],
"propertyType":"attribute",
"attributeName":{
"localPart":"x",
"namespaceURI":""
}
},
"y":{
"title":"y",
"allOf":[
{
"$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
}
],
"propertyType":"attribute",
"attributeName":{
"localPart":"y",
"namespaceURI":""
}
}
This is one JSON Schema excerpt from my XSD file. What I want is an Object that looks like this:
{id:"", x: "", y: ""}
The goal is to marshall this object then to XML.
Disclaimer: I'm the author of Jsonix.
If I got your question right, you're asking, how could you construct a JS object that would be marshallable with Jsonix then.
The trick most people do is to take an XML "this is how it should look like", unmarshal it and the log JSON.stringify(myObject, null 2), something like that. Then you'll see how the appropriate JS should look like.
What you could also do is to generate an JSON Schema out of your XML Schema. This will get you a JSON Schema like this one, providing the complete description of the JSON you need for marshalling.
I also consider implementing something like generation of the TypeScript classes, but that's future music.
Update
I'm sorry, I'm still not quite sure what your question is. You've added an JSON Schema which defines a NodeType complex type with a string id and decimal x and y properties.
You're asking what an empty object for this would be. Well, an empty object for your complex type would be just {}.
An object with values would be for instance {id:"someId", x: 4, y:2}.
But in XML you can't marshal just a complex type, you marshal an element of complex type. Which is represented in a form of a { name: ..., value: ... }. So you'd probably have something like:
{
"name": {
"namespaceURI": "uri",
"localPart": "root"
},
"value": {
"id" : "someId",
"x" : 4,
"y" : 2,
"TYPE_NAME": "NodeType"
}
}
Hope this helps.
To clarify this issue a bit more: Javascript is a loosely typed language. Even though a JS class has a prototype structure, a JS object has that structure only when its members are populated with values. So Jsonix generating JS mappings from XSD and then the JS script instantiating a new JS object from the mappings doesn't create an unpopulated structure. Only when the object is populated with values is the structure created. That population can be done directly in code by assigning values to the new object, drilling down its tree of contained members populating each one after that member is added to the complex overall JS object, the programmer using the XSD or its docs as a guide. Or the complex overall JS object can be populated by a JSON block, in which case the JSON can be generated from XML of the complex object unmarshaled by Jsonix. The unmarshaling (and any marshaling) can be executed by Jsonix. The bottom line is that either existing XML of the complex object to be un/marshaled, or line by line assignments in the implicit structure must be supplied to the code that uses Jsonix for its features. It's not like in Java (eg. JAXB) where an XSD can be used to generate a new complex object with all its structure that is then populated by the Java program (by assignments in the tree or by unmarshaling from XML).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With