Topic
  • 1 reply
  • Latest Post - ‏2011-10-10T14:58:45Z by SystemAdmin
SystemAdmin
SystemAdmin
1138 Posts

Pinned topic Need help with creating XML Schema

‏2011-10-10T02:54:20Z |
I am working on creating an xml schema for the following xml format.
The problem I have is, all the elements are identified by <Attribute name = "blah blah" ....
and I want to know how I can validate this kind of xml, since I don't have specific tag names for each element.

<Object type="obj.com.bmssolutions.WSPInput">
<Attribute name="Type" value="xxxxComponent" />
<Attribute name="Operation" value="CreateRecord" />
<!-- could have 1 to multiple datafields -->
<Attribute name="DataFields">
<Object type="xxxxDataField">
<Attribute name="ObjectAttribute" value="xxxx_3508" />
<Attribute name="AttributeType" value="Text" />
<Attribute name="Value" value="is auto number" />
</Object>
</Attribute>

<!-- only 1 locator -->
<Attribute name="DataLocator">
<Locator type="obj.com.bmssolutions.COMPONENT.bms_9999999_109" id="xxx">
<Attribute>xxxxx_3508</Attribute>
<Attribute>xxxx_1131</Attribute>
<Attribute>xxxxx_3512</Attribute>
<Attribute>xxxxx_1696</Attribute>
<Attribute>xxxxx_1695</Attribute>
<Attribute>xxxxx_1130</Attribute>
</Locator>
</Attribute>

</Object>

Could some one have a look at this and see if this can be validated with an xsd ?
I tried to autogenerate the xsd with some tools, visual studio / Oxygen, but they are all different and
did not really help. any help is much appreciated.

cheers
Praveen
Updated on 2011-10-10T14:58:45Z at 2011-10-10T14:58:45Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    1138 Posts

    Re: Need help with creating XML Schema

    ‏2011-10-10T14:58:45Z  
    [0] If you have no complete control over the authoring of the xml instance, I would suggest that construction of the document is not readily available for the validation in w3c schema language. And therefore, if validation is critical, you've to persuade the other party to draft the structure otherwise, such as making the elements named Type, Operation, DataFields etc...

    [0.1] There is a grand asymmetry in the w3c schema language, at least, between element/attribute name and text content the same. Attempt to switch over one to another is begging trouble. Since it is an all the same conceivable document, one way to say it is that the w3c schema language is not expressive enough to handle this kind of document.

    [0.2] If you have complete control over the authoring of the xml instance, you can do some "radical" addition to make it more forthcoming for validation by wxsl, as I will show a solution for it. But, at the outset, I have to mention that the validation on the facet - the cardinality of DataFields and DataLocator and in fact any other kinds is still remaining impossible then.

    [1] The document instance should explicitly assert its type using the attribute xsi:type. The value of xsi:type is intimately related to the schema, see below.

    [1.1] This is how it would look like for the schema construction used in [2].
    
    <Object type=
    "obj.com.bmssolutions.WSPInput" xmlns:xsi=
    "http://www.w3.org/2001/XMLSchema-instance"> <Attribute name=
    "Type" value=
    "xxxxComponent" xsi:type=
    "Type" /> <Attribute name=
    "Operation" value=
    "CreateRecord" xsi:type=
    "Operation" /> <!-- could have 1 to multiple datafields --> <Attribute name=
    "DataFields" xsi:type=
    "DataFields"> <Object type=
    "xxxxDataField"> <Attribute name=
    "ObjectAttribute" value=
    "xxxx_3508" xsi:type=
    "ObjectAttribute" /> <Attribute name=
    "AttributeType" value=
    "Text" xsi:type=
    "AttributeType" /> <Attribute name=
    "Value" value=
    "is auto number" xsi:type=
    "Value" /> </Object> </Attribute> <!-- only 1 locator --> <Attribute name=
    "DataLocator" xsi:type=
    "DataLocator"> <Locator type=
    "obj.com.bmssolutions.COMPONENT.bms_9999999_109" id=
    "xxx"> <Attribute>xxxxx_3508</Attribute> <Attribute>xxxx_1131</Attribute> <Attribute>xxxxx_3512</Attribute> <Attribute>xxxxx_1696</Attribute> <Attribute>xxxxx_1695</Attribute> <Attribute>xxxxx_1130</Attribute> </Locator> </Attribute> </Object>
    

    [2] This is how the schema would look like. It would involve quite a selection of constructions not exactly as easy as it seems to make it work together.
    
    <xs:schema version=
    "1.0" xmlns:xs=
    "http://www.w3.org/2001/XMLSchema">   <xs:complexType name=
    "abstractAttributeType" abstract=
    "true" />   <xs:complexType name=
    "Type"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:attribute name=
    "name" use=
    "required" fixed=
    "Type" /> <xs:attribute name=
    "value" type=
    "xs:string" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "Operation"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:attribute name=
    "name" use=
    "required" fixed=
    "Operation" /> <xs:attribute name=
    "value" type=
    "xs:string" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "ObjectAttribute"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:attribute name=
    "name" use=
    "required" fixed=
    "ObjectAttribute" /> <xs:attribute name=
    "value" type=
    "xs:string" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "AttributeType"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:attribute name=
    "name" use=
    "required" fixed=
    "AttributeType" /> <xs:attribute name=
    "value" type=
    "xs:string" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "Value"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:attribute name=
    "name" use=
    "required" fixed=
    "Value" /> <xs:attribute name=
    "value" type=
    "xs:string" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "DataFields"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:sequence> <xs:element name=
    "Object" type=
    "ObjectType" /> </xs:sequence> <xs:attribute name=
    "name" use=
    "required" fixed=
    "DataFields" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "ObjectType"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:sequence> <xs:element name=
    "Attribute" type=
    "abstractAttributeType" minOccurs=
    "3" maxOccurs=
    "3" /> </xs:sequence> <xs:attribute name=
    "type" type=
    "xs:string" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:complexType name=
    "DataLocator"> <xs:complexContent> <xs:extension base=
    "abstractAttributeType"> <xs:sequence> <xs:element name=
    "Locator"> <xs:complexType> <xs:sequence> <xs:element name=
    "Attribute" maxOccurs=
    "unbounded" type=
    "xs:string" /> </xs:sequence> <xs:attribute name=
    "type" type=
    "xs:string" /> <xs:attribute name=
    "id" type=
    "xs:string" /> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name=
    "name" use=
    "required" fixed=
    "DataLocator" /> </xs:extension> </xs:complexContent> </xs:complexType>   <xs:element name=
    "Object"> <xs:complexType> <xs:sequence> <xs:element name=
    "Attribute" type=
    "abstractAttributeType" maxOccurs=
    "unbounded" /> </xs:sequence> <xs:attribute name=
    "type" type=
    "xs:string" /> </xs:complexType> </xs:element>   </xs:schema>