?
Solved

XML + XSD "required"?

Posted on 2004-04-08
4
Medium Priority
?
556 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.



0
Comment
Question by:chuckie_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 10785416
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
0
 

Author Comment

by:chuckie_
ID: 10878792
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!
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 200 total points
ID: 10881259
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;
xmlDoc.load(XmlUri);

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:

http://rdcpro.com/Members/rdcpro/snippets/xsltandnamespaces/

Regards,
Mike Sharp
0
 

Author Comment

by:chuckie_
ID: 10887405
I see what you are saying, crystal clear. thanks for your help.
No doubt I'll be posting a lot more questions
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question