Validating xml file with schema.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<P>Shraddha Gurav wrote : </P>
<P>My requirement is somewhat like this:</P>
<P>Suppose this is my xml file, which I want to validate using schema.</P>
<P><myroot>
<married>yes</married>
<dateofwedding>1973-10-12</dateofwedding>
</myroot></P>
<P>
I would love to have the possibility of having something like this in XML
schema: (look at the condition attributes in the dateofwedding element)

<xsd:element name="myroot
<xsd:complexType>
<xsd:element name="married
<xsd:simpleType>
<xsd:restriction base="xsd:string
<xsd:enumeration value="yes"/>
<xsd:enumeration value="no"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="dateofwedding" type="xsd:date" condition="if married=yes" then="minOccurs="1""
else="minOccurs="0"")/>
</xsd:complexType>
</xsd:element></P>
<P>If the value of <married> element is "yes", then only it can have the <dateofwedding> element within it (minOccurs="1"), but if the <married> element value is "no" then it can not have the <dateofwedding> element (minOccurs="0").</P>
<P>I want to know how to use this conditions using schema.
I tried to write schema like this: but it shows the following error message. </P>
<P>Schema Error:
System.Xml.Schema.XmlSchemaException: The http://www.w3.org/2001/XMLSchema:schema element is not supported in this context. at System.Xml.Schema.XmlSchemaCollection.SendValidationEvent(XmlSchemaException e) at System.Xml.Schema.XmlSchemaCollection.Add(String ns, XmlReader reader, XmlResolver resolver) at System.Xml.Schema.XmlSchemaCollection.Add(String ns, XmlReader reader) at Validator.Button1_Click(Object sender, EventArgs e)</P>
<P>
<xsd:element name="married
<xsd:simpleType>
<xsd:restriction base="xsd:string
<xsd:enumeration value="yes
<mrd:schema id="mrd" xmlns:mrd=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema
<!-- Here "mrd" prefix is used to specify "married" -->
<mrd:complexType>
<mrd:sequence>
<mrd:element name="dateofwedding" type="mrd:date" minOccurs="1" maxOccurs="1"/>
</mrd:sequence>
<mrd:complexType>
</mrd:schema> </xsd:enumeration>
<xsd:enumeration value="no"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element> </P>
<P>
I have seen another way of doing this thing at this URL : Please refer to it.
http://lists.xml.org/archives/xml-dev/200005/msg00741.html http://lists.xml.org/archives/xml-dev/200005/msg00741.html http://lists.xml.org/archives/xml-dev/200005/msg00741.html </P>
<P>But even this did not work.</P>
<P> </P>
<P> </P>
<P>
</P>

View the full article
 
Back
Top