Sign in to follow this  

XML Schema help

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a tool that parses an XML file. I'm trying to use xml schemas to validate the XML before I parse it. I'm having trouble making a schema that fits the type of XML I need. It's a simple problem, but I'm having a brain fart. Here is the XML I have:
<?xml version="1.0" encoding="UTF-8"?>
<ShapeConfigFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="X:\tools\bin\acs_schema.xsd" version="1" >
	<ShapeConfig ShapeName="fist_left" >
		<Sphere radius="1.0" />
	</ShapeConfig>
	<ShapeConfig ShapeName="fist_right">
		<Sphere radius="1.0" />
	</ShapeConfig>
</ShapeConfigFile>



Here is the schema I have.
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:element name="ShapeConfigFile" type="ShapeConfigFileType" />
	<xsd:complexType name="ShapeConfigFileType">
		<xsd:sequence>
			<xsd:element name="ShapeConfig" type="ShapeConfigType" />
		</xsd:sequence>
		<xsd:attribute name="version" type="xsd:integer" />
	</xsd:complexType>
	<xsd:complexType name="ShapeSphere">
		<xsd:attribute name="radius" type="xsd:float" />
	</xsd:complexType>
	<xsd:complexType name="ShapeCapsule">
		<xsd:attribute name="radius" type="xsd:float" />
		<xsd:attribute name="length" type="xsd:float" />
	</xsd:complexType>
	<xsd:complexType name="ShapeConfigType">
		<xsd:sequence maxOccurs="256">
			<xsd:choice maxOccurs="1" minOccurs="1">
				<xsd:element name="Sphere" type="ShapeSphere" />
				<xsd:element name="Capsule" type="ShapeCapsule" />
			</xsd:choice>
		</xsd:sequence>
		<xsd:attribute name="ShapeName" type="xsd:string" />
	</xsd:complexType>
</xsd:schema>


When I load the XML with the schema, it errors out on the second ShapeConfig. I'm not sure why it's not allowing multiple ShapeConfigs :( Any help? Thanks

Share this post


Link to post
Share on other sites
This seems to work (added a maxOccurs="unbounded" to ShapeConfig).


<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ShapeConfigFile" type="ShapeConfigFileType"/>
<xsd:complexType name="ShapeConfigFileType">
<xsd:sequence>
<xsd:element name="ShapeConfig" type="ShapeConfigType" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="version" type="xsd:integer"/>
</xsd:complexType>
<xsd:complexType name="ShapeSphere">
<xsd:attribute name="radius" type="xsd:float"/>
</xsd:complexType>
<xsd:complexType name="ShapeCapsule">
<xsd:attribute name="radius" type="xsd:float"/>
<xsd:attribute name="length" type="xsd:float"/>
</xsd:complexType>
<xsd:complexType name="ShapeConfigType">
<xsd:sequence maxOccurs="256">
<xsd:choice maxOccurs="1" minOccurs="1">
<xsd:element name="Sphere" type="ShapeSphere"/>
<xsd:element name="Capsule" type="ShapeCapsule"/>
</xsd:choice>
</xsd:sequence>
<xsd:attribute name="ShapeName" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>


IIRC, a sequence is just a way of saying "these elements have to occur as children in this specific order", it's not "allow a sequence of these structures". In order to allow multiple children of the same type, you have to say so explicitly.

Share this post


Link to post
Share on other sites

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this