We help IT Professionals succeed at work.

XML + XSD "required"?

Medium Priority
674 Views
Last Modified: 2013-11-19
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.



Comment
Watch Question

Commented:
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:

http://xfront.com

Regards,
Mike Sharp

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"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="person.xsd">

or
<person ID="1" DOB="f" isMale="true"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="nosuchfile.xsd">

the namespaceSchemaLocation does not make any difference!
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I see what you are saying, crystal clear. thanks for your help.
No doubt I'll be posting a lot more questions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.