public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="northwestStates"> XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(); simpleType.Name = "northwestStates"; schema.Items.Add(simpleType); // <xs:annotation> XmlSchemaAnnotation annNorthwestStates = new XmlSchemaAnnotation(); simpleType.Annotation = annNorthwestStates; // <xs:documentation>States in the Pacific Northwest of US</xs:documentation> XmlSchemaDocumentation docNorthwestStates = new XmlSchemaDocumentation(); annNorthwestStates.Items.Add(docNorthwestStates); docNorthwestStates.Markup = TextToNodeArray("States in the Pacific Northwest of US"); // <xs:restriction base="xs:string"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:enumeration value="WA"> XmlSchemaEnumerationFacet enumerationWA = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumerationWA); enumerationWA.Value = "WA"; // <xs:annotation> XmlSchemaAnnotation annWA = new XmlSchemaAnnotation(); enumerationWA.Annotation = annWA; // <xs:documentation>Washington</documentation> XmlSchemaDocumentation docWA = new XmlSchemaDocumentation(); annWA.Items.Add(docWA); docWA.Markup = TextToNodeArray("Washington"); // <xs:enumeration value="OR"> XmlSchemaEnumerationFacet enumerationOR = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumerationOR); enumerationOR.Value = "OR"; // <xs:annotation> XmlSchemaAnnotation annOR = new XmlSchemaAnnotation(); enumerationOR.Annotation = annOR; // <xs:documentation>Oregon</xs:documentation> XmlSchemaDocumentation docOR = new XmlSchemaDocumentation(); annOR.Items.Add(docOR); docOR.Markup = TextToNodeArray("Oregon"); // <xs:enumeration value="ID"> XmlSchemaEnumerationFacet enumerationID = new XmlSchemaEnumerationFacet(); restriction.Facets.Add(enumerationID); enumerationID.Value = "ID"; // <xs:annotation> XmlSchemaAnnotation annID = new XmlSchemaAnnotation(); enumerationID.Annotation = annID; // <xs:documentation>Idaho</xs:documentation> XmlSchemaDocumentation docID = new XmlSchemaDocumentation(); annID.Items.Add(docID); docID.Markup = TextToNodeArray("Idaho"); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
/// <summary> /// Creates an XML schema element for reading a value of the given type /// </summary> /// <param name="Name">Name of the field</param> /// <param name="Type">Type of the field</param> /// <returns>New schema element representing the field</returns> static XmlSchemaElement CreateSchemaFieldElement(string Name, Type Type) { XmlSchemaElement Element = new XmlSchemaElement(); Element.Name = Name; Element.MinOccurs = 0; Element.MaxOccurs = 1; if (Type == typeof(string)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; } else if (Type == typeof(bool)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Boolean).QualifiedName; } else if (Type == typeof(int)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Int).QualifiedName; } else if (Type == typeof(float)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Float).QualifiedName; } else if (Type == typeof(double)) { Element.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Double).QualifiedName; } else if (Type.IsEnum) { XmlSchemaSimpleTypeRestriction Restriction = new XmlSchemaSimpleTypeRestriction(); Restriction.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; foreach (string EnumName in Enum.GetNames(Type)) { XmlSchemaEnumerationFacet Facet = new XmlSchemaEnumerationFacet(); Facet.Value = EnumName; Restriction.Facets.Add(Facet); } XmlSchemaSimpleType EnumType = new XmlSchemaSimpleType(); EnumType.Content = Restriction; Element.SchemaType = EnumType; } else if (Type == typeof(string[])) { XmlSchemaElement ItemElement = new XmlSchemaElement(); ItemElement.Name = "Item"; ItemElement.SchemaTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName; ItemElement.MinOccurs = 0; ItemElement.MaxOccursString = "unbounded"; XmlSchemaSequence Sequence = new XmlSchemaSequence(); Sequence.Items.Add(ItemElement); XmlSchemaComplexType ArrayType = new XmlSchemaComplexType(); ArrayType.Particle = Sequence; Element.SchemaType = ArrayType; } else { throw new Exception("Unsupported field type for XmlConfigFile attribute"); } return(Element); }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="SizeType"> XmlSchemaSimpleType SizeType = new XmlSchemaSimpleType(); SizeType.Name = "SizeType"; // <xs:restriction base="xs:string"> XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // <xs:enumeration value="Small"/> XmlSchemaEnumerationFacet enumerationSmall = new XmlSchemaEnumerationFacet(); enumerationSmall.Value = "Small"; restriction.Facets.Add(enumerationSmall); // <xs:enumeration value="Medium"/> XmlSchemaEnumerationFacet enumerationMedium = new XmlSchemaEnumerationFacet(); enumerationMedium.Value = "Medium"; restriction.Facets.Add(enumerationMedium); // <xs:enumeration value="Large"/> XmlSchemaEnumerationFacet enumerationLarge = new XmlSchemaEnumerationFacet(); enumerationLarge.Value = "Large"; restriction.Facets.Add(enumerationLarge); SizeType.Content = restriction; schema.Items.Add(SizeType); // <xs:element name="Item"> XmlSchemaElement elementItem = new XmlSchemaElement(); elementItem.Name = "Item"; // <xs:complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType(); // <xs:attribute name="Size" type="SizeType"/> XmlSchemaAttribute attributeSize = new XmlSchemaAttribute(); attributeSize.Name = "Size"; attributeSize.SchemaTypeName = new XmlQualifiedName("SizeType", ""); complexType.Attributes.Add(attributeSize); elementItem.SchemaType = complexType; schema.Items.Add(elementItem); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
protected override XmlSchemaType RamlTypeToSchemaType(RamlType ramlType, ConversionOptions options) { if (ramlType == null) { return(null); } XmlSchemaType xmlSchemaType; // check if it is enum if (ramlType.Enum != null) { xmlSchemaType = new XmlSchemaSimpleType(); xmlSchemaType.Name = ramlType.Name; var restriction = new XmlSchemaSimpleTypeRestriction(); var enumElementTypeName = RamlDataTypeToXmlSchemaDataType(ramlType.Enum.ItemsTypeName); if (enumElementTypeName == null) { enumElementTypeName = GetXmlRefDataType(ramlType.Enum.ItemsTypeName, options.XmlNamespace); } if (enumElementTypeName != null) { restriction.BaseTypeName = enumElementTypeName; } foreach (string enumValue in ramlType.Enum.EnumValues) { var enumerationFacet = new XmlSchemaEnumerationFacet(); enumerationFacet.Value = enumValue; restriction.Facets.Add(enumerationFacet); } ((XmlSchemaSimpleType)xmlSchemaType).Content = restriction; return(xmlSchemaType); } xmlSchemaType = new XmlSchemaComplexType(); xmlSchemaType.Name = ramlType.Name; // check if it is array if (ramlType.Array != null) { var arraySequence = new XmlSchemaSequence(); var arrayElement = new XmlSchemaElement(); arrayElement.Name = ramlType.Array.ItemName; arrayElement.MaxOccursString = "unbounded"; var arrayElementTypeName = RamlDataTypeToXmlSchemaDataType(ramlType.Array.ItemsTypeName); if (arrayElementTypeName == null) { arrayElementTypeName = GetXmlRefDataType(ramlType.Array.ItemsTypeName, options.XmlNamespace); } if (arrayElementTypeName != null) { arrayElement.SchemaTypeName = arrayElementTypeName; } arraySequence.Items.Add(arrayElement); ((XmlSchemaComplexType)xmlSchemaType).Particle = arraySequence; return(xmlSchemaType); } // check if it has base if (ramlType.Base != null) { // assume simple type for now xmlSchemaType = new XmlSchemaSimpleType(); xmlSchemaType.Name = ramlType.Name; var restriction = new XmlSchemaSimpleTypeRestriction(); var baseTypeName = RamlDataTypeToXmlSchemaDataType(ramlType.Base.Name); if (baseTypeName == null) { baseTypeName = GetXmlRefDataType(ramlType.Base.Name, options.XmlNamespace); } if (baseTypeName != null) { restriction.BaseTypeName = baseTypeName; } if (!string.IsNullOrEmpty(ramlType.Base.Pattern)) { var pattern = new XmlSchemaPatternFacet(); pattern.Value = ramlType.Base.Pattern; restriction.Facets.Add(pattern); } ((XmlSchemaSimpleType)xmlSchemaType).Content = restriction; return(xmlSchemaType); } var annotation = GetXmlAnnotattion(ramlType.Description); if (annotation != null && options.GenerateDescriptions) { xmlSchemaType.Annotation = annotation; } var sequence = new XmlSchemaSequence(); ((XmlSchemaComplexType)xmlSchemaType).Particle = sequence; if (ramlType.Properties == null || ramlType.Properties.Count == 0) { return(xmlSchemaType); } foreach (RamlProperty ramlProperty in ramlType.Properties) { var xmlSchemaProperty = new XmlSchemaElement(); xmlSchemaProperty.Name = ramlProperty.Name; var typeName = RamlDataTypeToXmlSchemaDataType(ramlProperty.Type); if (typeName == null) { typeName = GetXmlRefDataType(ramlProperty.Type, options.XmlNamespace); } if (typeName != null) { xmlSchemaProperty.SchemaTypeName = typeName; } var propertyAnnotation = GetXmlAnnotattion(ramlProperty.Description); if (propertyAnnotation != null && options.GenerateDescriptions) { xmlSchemaProperty.Annotation = propertyAnnotation; } sequence.Items.Add(xmlSchemaProperty); if (!ramlProperty.Required) { xmlSchemaProperty.MinOccurs = 0; xmlSchemaProperty.IsNillable = true; } } return(xmlSchemaType); }
internal XmlSchemaAttribute CreateXsdAttribute() { XmlSchemaAttribute a = new XmlSchemaAttribute(); SetLineInfo(a); a.Name = Name; a.DefaultValue = resolvedNormalizedDefaultValue; if (OccurenceType != DTDAttributeOccurenceType.Required) { a.Use = XmlSchemaUse.Optional; } XmlQualifiedName qname = XmlQualifiedName.Empty; ArrayList enumeration = null; if (enumeratedNotations != null && enumeratedNotations.Count > 0) { qname = new XmlQualifiedName("NOTATION", XmlSchema.Namespace); enumeration = enumeratedNotations; } else if (enumeratedLiterals != null) { enumeration = enumeratedLiterals; } else { switch (Datatype.TokenizedType) { case XmlTokenizedType.ID: qname = new XmlQualifiedName("ID", XmlSchema.Namespace); break; case XmlTokenizedType.IDREF: qname = new XmlQualifiedName("IDREF", XmlSchema.Namespace); break; case XmlTokenizedType.IDREFS: qname = new XmlQualifiedName("IDREFS", XmlSchema.Namespace); break; case XmlTokenizedType.ENTITY: qname = new XmlQualifiedName("ENTITY", XmlSchema.Namespace); break; case XmlTokenizedType.ENTITIES: qname = new XmlQualifiedName("ENTITIES", XmlSchema.Namespace); break; case XmlTokenizedType.NMTOKEN: qname = new XmlQualifiedName("NMTOKEN", XmlSchema.Namespace); break; case XmlTokenizedType.NMTOKENS: qname = new XmlQualifiedName("NMTOKENS", XmlSchema.Namespace); break; case XmlTokenizedType.NOTATION: qname = new XmlQualifiedName("NOTATION", XmlSchema.Namespace); break; } } if (enumeration != null) { XmlSchemaSimpleType st = new XmlSchemaSimpleType(); SetLineInfo(st); XmlSchemaSimpleTypeRestriction r = new XmlSchemaSimpleTypeRestriction(); SetLineInfo(r); r.BaseTypeName = qname; if (enumeratedNotations != null) { foreach (string name in enumeratedNotations) { XmlSchemaEnumerationFacet f = new XmlSchemaEnumerationFacet(); SetLineInfo(f); r.Facets.Add(f); f.Value = name; } } st.Content = r; } else if (qname != XmlQualifiedName.Empty) { a.SchemaTypeName = qname; } return(a); }
static XmlQualifiedName AddAttributeTypeToXmlSchema(SchemaInfo schemaInfo, UxmlAttributeDescription description, IUxmlFactory factory, FactoryProcessingHelper processingData) { if (description.name == null) { return(null); } string attrTypeName = factory.uxmlQualifiedName + "_" + description.name + "_" + k_TypeSuffix; string attrTypeNameInBaseElement = factory.substituteForTypeQualifiedName + "_" + description.name + "_" + k_TypeSuffix; FactoryProcessingHelper.AttributeRecord attrRecord; if (processingData.attributeTypeNames.TryGetValue(attrTypeNameInBaseElement, out attrRecord)) { // If restriction != baseElement.restriction, we need to declare a new type. // Note: we do not support attributes having a less restrictive restriction than its base type. if ((description.restriction == null && attrRecord.desc.restriction == null) || (description.restriction != null && description.restriction.Equals(attrRecord.desc.restriction))) { // Register attrTypeName -> attrRecord for potential future derived elements. processingData.attributeTypeNames.Add(attrTypeName, attrRecord); return(attrRecord.name); } } XmlQualifiedName xqn; FactoryProcessingHelper.AttributeRecord attributeRecord; if (description.restriction == null) { // Type is a built-in type. xqn = new XmlQualifiedName(description.type, description.typeNamespace); attributeRecord = new FactoryProcessingHelper.AttributeRecord { name = xqn, desc = description }; processingData.attributeTypeNames.Add(attrTypeName, attributeRecord); return(xqn); } string attrTypeNameForSchema = factory.uxmlName + "_" + description.name + "_" + k_TypeSuffix; xqn = new XmlQualifiedName(attrTypeNameForSchema, schemaInfo.schema.TargetNamespace); XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(); simpleType.Name = attrTypeNameForSchema; UxmlEnumeration enumRestriction = description.restriction as UxmlEnumeration; if (enumRestriction != null) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace); foreach (var v in enumRestriction.values) { XmlSchemaEnumerationFacet enumValue = new XmlSchemaEnumerationFacet(); enumValue.Value = v; restriction.Facets.Add(enumValue); } } else { UxmlValueMatches regexRestriction = description.restriction as UxmlValueMatches; if (regexRestriction != null) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace); XmlSchemaPatternFacet pattern = new XmlSchemaPatternFacet(); pattern.Value = regexRestriction.regex; restriction.Facets.Add(pattern); } else { UxmlValueBounds bounds = description.restriction as UxmlValueBounds; if (bounds != null) { XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); simpleType.Content = restriction; restriction.BaseTypeName = new XmlQualifiedName(description.type, description.typeNamespace); XmlSchemaFacet facet; if (bounds.excludeMin) { facet = new XmlSchemaMinExclusiveFacet(); } else { facet = new XmlSchemaMinInclusiveFacet(); } facet.Value = bounds.min; restriction.Facets.Add(facet); if (bounds.excludeMax) { facet = new XmlSchemaMaxExclusiveFacet(); } else { facet = new XmlSchemaMaxInclusiveFacet(); } facet.Value = bounds.max; restriction.Facets.Add(facet); } else { Debug.Log("Unsupported restriction type."); } } } schemaInfo.schema.Items.Add(simpleType); attributeRecord = new FactoryProcessingHelper.AttributeRecord { name = xqn, desc = description }; processingData.attributeTypeNames.Add(attrTypeName, attributeRecord); return(xqn); }
private XmlSchemaSimpleTypeRestriction ExtractStringFacets(JsonSchema jSchema) { XmlSchemaSimpleTypeRestriction content = new XmlSchemaSimpleTypeRestriction { BaseTypeName = new XmlQualifiedName("string", XML_SCHEMA_NS), }; EnumKeyword enumKeyword = jSchema.Get <EnumKeyword>(); if (enumKeyword != null) { foreach (JsonValue enumValue in GetterExtensions.Enum(jSchema)) { XmlSchemaEnumerationFacet enumFacet = new XmlSchemaEnumerationFacet { Value = enumValue.String, }; content.Facets.Add(enumFacet); } } MinLengthKeyword minLength = jSchema.Get <MinLengthKeyword>(); MaxLengthKeyword maxLength = jSchema.Get <MaxLengthKeyword>(); if (minLength != null && maxLength != null && minLength.Value == maxLength.Value) { // special rule that maps equal min and max lengths to xsd length facet XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet { Value = minLength.Value.ToString(), }; content.Facets.Add(lengthFacet); } else { if (minLength != null) { XmlSchemaMinLengthFacet minLengthFacet = new XmlSchemaMinLengthFacet { Value = minLength.Value.ToString(), }; content.Facets.Add(minLengthFacet); } if (maxLength != null) { XmlSchemaMaxLengthFacet maxLengthFacet = new XmlSchemaMaxLengthFacet { Value = maxLength.Value.ToString(), }; content.Facets.Add(maxLengthFacet); } } PatternKeyword pattern = jSchema.Get <PatternKeyword>(); if (pattern != null) { XmlSchemaPatternFacet patternFacet = new XmlSchemaPatternFacet { Value = pattern.Value.ToString(), }; content.Facets.Add(patternFacet); } FormatKeyword format = jSchema.Get <FormatKeyword>(); if (format != null && format.Value != null && !string.IsNullOrEmpty(format.Value.Key)) { content.BaseTypeName = ExtractBaseTypeNameFromFormat(format.Value.Key); } return(content); }