0

Following instructions on using jsonix-schema-compiler I successfully got an mapping object for a xsd-file; the very summarized content of which is:

var IdentPerson_Module_Factory = function () {
  var IdentPerson = {
    name: 'IdentPerson',
    defaultElementNamespaceURI: 'http:\/\/www.some.domain.de\/mynamespace',
    typeInfos: [{
      ....
      ....
     }],
    elementInfos: [{
        elementName: 'Person',
        typeInfo: '.Person'
     }]
  };
  return {
    IdentPerson: IdentPerson
  };
};

Now I want to produce an xml-String by using jsonix and the above json-mapping-object:

var context = new Jsonix.Context([IdentPerson]);
var marshaller = context.createMarshaller();
var xmldoc = marshaller.marshalString(myJsonString);

First lines of myJsonString are as following:

{ Person:
  { aliasName:
   { titel: '',
     namenssuffix: '',
     familyname: [Object],
 .....
 .....
}

Ending up with the error:

Message:  Element [Person] is not known in this context, could not determine its type.
Stack: Error: Element [Person] is not known in this context, could not determine its type.
at Object.Jsonix.Binding.Marshalls.Element.Jsonix.Class.marshalElement (/home/datarocket/datarocket.hub/src/node_modules/jsonix/jsonix.js:1881:10)

I guess it is because of missing namespace in the myJsonString? If so, how can I fix it? Thanks in advance;

saab
  • 129
  • 1
  • 3
  • 16

1 Answers1

0

Your mapping specifies a namespace but your JSON object does not.

Try:

var context = new Jsonix.Context(mappings, {
    namespacePrefixes: {
        "http://www.some.domain.de/mynamespace": "tns"
    }
});

{ 'tns:Person': ... }

Or:

var context = new Jsonix.Context(mappings, {
    namespacePrefixes: {
        "http://www.some.domain.de/mynamespace": ""
    }
});

{ Person: ... }

Please see documentation on the simplified mapping style, it has a hint for exactly this case.

Disclaimer: I'm the author of Jsonix.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Yes, it was helpful for a simple xsd file with only one ns. However my root element "Person" consists of several children, sub-children, sub-sub-children etc., each of which with different namespaces. In the mapping object (created by jsonix-schema-compiler) these namespaces are declared as: namespaceURI:'blabla'. Their prefixes are not listed. Now when creating jsonix-context I define the map: namespacePrefixes:{.....} . with all of those ns's. Result: Stack: Error: Element [Person] is not known in this context, could not determine its type. – saab Jul 12 '16 at 16:08
  • @saab Well, you have to declare namespace prefixes and use them in your element names or use the full for (`{name: ..., value:}`). See the [docs](https://github.com/highsource/jsonix/wiki/Properties#element-reference-property). – lexicore Jul 12 '16 at 19:01
  • Finally I successfully solved the problem! As you mentioned my mistake was not using namespace prefixes in my json-document while I was creating the context passing it a map of namespaces. Thank you!! :) – saab Jul 12 '16 at 22:52