XML + XSD "required"?

I'm just learning XML and I am trying to write a simple schema in XSD. I have looked over a few web tutorials and I have checked a book I have but I can't seem to fix this simple but annoying problem. When I define the schema I set some attributes and elements to "required" but if I leave them blank in my xml file it doesn't affect it. should it throw an error? Also when defining attributes it seems the rules I have set are ignored
e.g. if I have  
<xsd:attribute name="DOB" type="xsd:date" use="required"/>
My way of looking at it means I must have an attribute DOB("required") and it must be of type date but if I leave it out or set DOB="x" there is no error thrown.
It is probably a very obvious silly mistake so any help would be appreciated.

Who is Participating?
rdcproConnect With a Mentor Commented:
Parsing is simply reading in the XML or XSLT (they're both XML structures).  You always have to parse the XML.  XSLT processing isn't parsing, though you must first parse the XML to do the transformation.  

There are times when you need (or simply want) an XSD. For example, if you need to validate a document to be sure it conforms to a specific structure and set of datatypes.  Or if you want the schema to provide information (such as entity references) to the XML.  Or if you're trying to map an XML document to a relational database...stuff like that.

If you have no business requirement to validate (for example, you control the XML and XSLT yourself, and are confident it's correct) then having an XSD and validating with it is probably processing overhead you don't need.  

As far as the XSD reference goes, you can specify the schema, or not.  But simply specifying the schema doesn't require the parser to validate the document when it parses the XML.  In the Microsoft world, using MSXML, you tell the parser to validate like (assuming JScript on a browser client with MSXML 4.0 installed):

var xmlDoc = new ActiveXObject("Msxml2.DomDocument.4.0");
xmlDoc.validateOnParse = true;
xmlDoc.resolveExternals = true;

var myErr = xmlDoc.parseError
if (myErr.errorCode != 0)
          alert("You have error " + myErr.reason);

If you use a namespaceQualified Schema, using:

xsi:schemaLocation="urn:schemas-rdcpro-com:tempUri    nosuchfile.xsd">

(note there are two pieces to that attribute; the target namespace, and the schema location) your XML will be scoped to that namespace, and your XSLT will need to address this:


Mike Sharp
There is nothing that requires the parser to validate.   If you're using MSXML, you need to set a couple properties:

xmlDoc.validateOnParse = true
xmlDoc.resolveExternals = true  //not really needed, but I explicit set this anyway to be sure.

Other parsers will have their own methods, but they'll probably be similar.

If you're using an IDE like XML Spy, then there's probably something wrong with your schema, or how you've attached a reference to it in the XML document.  Post the whole thing, XML and schema.

For an *excellent* tutorial on Schema, check out Roger Costello's site:


Mike Sharp
chuckie_Author Commented:
So you are saying that there is no use for an xsd unless you are using a parser? I am planning on using XSLT or possibly parsing ,so is that when the xsd comes into play. My xml file is displaying correctly but it doesnt matter if the xsd reference is present or not.

i.e it doesn't make a difference if the xml file has :
<person ID="1" DOB="f" isMale="true"

<person ID="1" DOB="f" isMale="true"

the namespaceSchemaLocation does not make any difference!
chuckie_Author Commented:
I see what you are saying, crystal clear. thanks for your help.
No doubt I'll be posting a lot more questions
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.