Reputation: 69
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.
Upvotes: 0
Views: 314
Reputation: 43671
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);
Upvotes: 0