XML Schema and circular references
-
Clearly I'm relatively new to authoring xml documents. If I have an elment eg CarEngineCompnent and this element can have more CarEngineComponent within it, how do I represent this in an XML Schema? I'm confused as to what the schema should look like. ASP.NET can never fail as working with it is like fitting bras to supermodels - it's one pleasure after the next - David Wulff
-
Clearly I'm relatively new to authoring xml documents. If I have an elment eg CarEngineCompnent and this element can have more CarEngineComponent within it, how do I represent this in an XML Schema? I'm confused as to what the schema should look like. ASP.NET can never fail as working with it is like fitting bras to supermodels - it's one pleasure after the next - David Wulff
Senkwe Chanda wrote: how do I represent this in an XML Schema? ...like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="CarEngineComponent">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="CarEngineComponent"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>Although technically allowed, having a structure like this undermines one of the the benefits that an XSD (XML Schema) provides, namely, strong types. What this schema essentially says is that a CarEngineComponent is a mixed type element that can contain a value and another element. Although there's noting wrong with that from a purely structural point of view, most XML documents don't use that structure prefering instead ot have elements that contain data or other elements. A bettter structure could be to have <CarEngineComponents> (note the plural form of the word) that contain one or more <CarEngineComponent> types. The naming convention makes it clear that <CarEngineComponents> is a container for something else. Erik Westermann Author, Learn XML In A Weekend (October 2002)
-
Senkwe Chanda wrote: how do I represent this in an XML Schema? ...like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="CarEngineComponent">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="CarEngineComponent"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>Although technically allowed, having a structure like this undermines one of the the benefits that an XSD (XML Schema) provides, namely, strong types. What this schema essentially says is that a CarEngineComponent is a mixed type element that can contain a value and another element. Although there's noting wrong with that from a purely structural point of view, most XML documents don't use that structure prefering instead ot have elements that contain data or other elements. A bettter structure could be to have <CarEngineComponents> (note the plural form of the word) that contain one or more <CarEngineComponent> types. The naming convention makes it clear that <CarEngineComponents> is a container for something else. Erik Westermann Author, Learn XML In A Weekend (October 2002)
Thanks Erik. I'm trying to setup a schema for a situation where the level of containment is unknown and/or variable. For example, that CarEngineComponent could have a CarEngineComponent that itself contains another CarEngineComponent. I'm getting the feeling that perhaps I'm not thinking in an XML-like way (which I've surprisingly found is not as sraight forward as I'd thought) Thanks again, I'll go and rethink my document. Regards Senkwe ASP.NET can never fail as working with it is like fitting bras to supermodels - it's one pleasure after the next - David Wulff