0

So I have an xsd file like this.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="DocumentElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="Item">
                    <xs:complexType>
                        <xs:all>              
                            <xs:element name="Text_1" minOccurs="1" maxOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">   
                                        <xs:minLength value="2"/>                                               
                                        <xs:maxLength value="25"/>
                                        <xs:whiteSpace value="collapse"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="Text_2" minOccurs="0" maxOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:maxLength value="2000"/>
                                        <xs:whiteSpace value="collapse"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="Company" minOccurs="1" maxOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">   
                                        <xs:minLength value="2"/>                                               
                                        <xs:maxLength value="32"/>
                                        <xs:whiteSpace value="collapse"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="TaxCode" minOccurs="0" maxOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:maxLength value="25"/>
                                        <xs:whiteSpace value="collapse"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="Inherit" minOccurs="1" maxOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">                                       
                                        <xs:pattern value="0|1|true|false|True|False|TRUE|FALSE"/>
                                        <xs:maxLength value="5"/>
                                        <xs:whiteSpace value="collapse"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:all>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

From that xsd file, i execute this command to get js file, so i could export that element to json file.

java -jar node_modules/jsonix/lib/jsonix-schema-compiler-full.jar -generateJsonSchema -d mappings -p PTSchema files/

which is i got this result.

var PTSchema = {
    name: 'PTSchema',
    typeInfos: [{
        localName: 'DocumentElement.Item',
        typeName: null,
        propertyInfos: [{
            name: 'text1',
            required: true,
            elementName: {
              localPart: 'Text_1'
            }
          }, {
            name: 'text2',
            elementName: {
              localPart: 'Text_2'
            }
          }, {
            name: 'company',
            required: true,
            elementName: {
              localPart: 'Company'
            }
          }, {
            name: 'taxCode',
            elementName: {
              localPart: 'TaxCode'
            }
          }, {
            name: 'inherit',
            required: true,
            elementName: {
              localPart: 'Inherit'
            }
          }]
      }, {
        localName: 'DocumentElement',
        typeName: null,
        propertyInfos: [{
            name: 'item',
            required: true,
            collection: true,
            elementName: {
              localPart: 'Item'
            },
            typeInfo: '.DocumentElement.Item'
          }]
      }],
    elementInfos: [{
        elementName: {
          localPart: 'DocumentElement'
        },
        typeInfo: '.DocumentElement'
      }]
  };

I just want to get the restriction/data type (what do you call it?) on each element so it will appeared on that result.

{
            name: 'text1',
            required: true,
type: string,
minLength: 2,
maxLength: 25,
whiteSpace: 'collapse',
            elementName: {
              localPart: 'Text_1'
            }
          }

Is that possible?

I Already take a look at JSONIX: Get restrictions and default value for properties But it didn't help.

dityagow
  • 69
  • 12
  • What exactly do you mean by "data type"? – lexicore Apr 25 '18 at 08:57
  • the data dype on its element ` ` – dityagow Apr 25 '18 at 14:36
  • I'm the author of Jsonix. Sorry, I do not understand what you mean by "data type". A fragment of the XML Schema does not help, that's simply not available from the generated mapping file. Maybe you could explain what you are trying to achieve. – lexicore Apr 25 '18 at 17:34
  • an honor, it can be responded directly by you. I already edited the question. – dityagow Apr 25 '18 at 17:46
  • Please explain what do you actually try to achieve. So that we won't be solving a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – lexicore Apr 25 '18 at 17:48
  • Also, why did [this answer](https://stackoverflow.com/questions/29256036/jsonix-get-restrictions-and-default-value-for-properties#answer-32537157) not help? Maybe you could include some code to demonstrate your expectations. – lexicore Apr 25 '18 at 17:49
  • From [that answer](https://stackoverflow.com/questions/29256036/jsonix-get-restrictions-and-default-value-for-properties#answer-32537157) I only get the `required`, `minOccurs` and `maxOccurs`. I just want to get the `base` value in the mapping file. Is it possible using jsonix? – dityagow Apr 25 '18 at 18:06
  • What's `base`? Are you interested in `typeInfo`? Since that's what actually defines the type of the property. – lexicore Apr 25 '18 at 18:11
  • Yes the `base` value from `` which is i expect `string` type on that element, so I could get it from mapping file – dityagow Apr 25 '18 at 18:19
  • Ok. Then check `typeInfo`. If it's omitted, it's `String`. – lexicore Apr 25 '18 at 18:21
  • Thats the problem i couldn't get it on the mapping file, there was no `typeInfo` attribute on that element `Text_1` based on my xsd file above. – dityagow Apr 25 '18 at 18:26

1 Answers1

0

As far as I understood you, you want to find the type of the property.

First, please read about properties in Jsonix. There are different types of properties. Some of them are single-typed (like value property or attribute property), some allow different type (like the elements property).

Anyway, no matter whether single-typed or heterogeneous, property definitions in Jsonix mappings use the typeInfo attribute to specify the type (or one of types) of the property.

See Types.

If the typeInfo is missing in the mapping, then the type is defaulted to String.

If the typeInfo starts with ., it references some other type in the same mapping module (module name is omitted).

You can read all this information from the mapping (which is just at JSON file). Alternatively, you can access this information from the Jsonix context after it is built.

For example, take this mapping:

var GH111 = {
    name: "GH111",
    dens: "urn:gh111",
    typeInfos: [{
            localName: "Root",
            propertyInfos: [{
                    name: "value",
                    type: "elements",
                    elementTypeInfos: [{
                        elementName: "a",
                        typeInfo: "String"
                    }, {
                        elementName: "b",
                        typeInfo: "Integer"
                    }]
                }]
        }],
    elementInfos: [{
            elementName: "root",
            typeInfo: ".Root"
        }]

};
module.exports.GH111 = GH111;

You can find the Root.value property and check what types it contains:

    var context = new Jsonix.Context([GH111], {
        namespacePrefixes : {
            "urn:test" : ""
        }
    });
    var rootType = context.getTypeInfoByName("GH111.Root");
    test.equal('urn:gh111', rootType.getPropertyInfoByName("value").elementTypeInfos[0].elementName.namespaceURI);
    test.equal('Integer', rootType.getPropertyInfoByName("value").elementTypeInfos[1].typeInfo.name);
lexicore
  • 42,748
  • 17
  • 132
  • 221