Solved

XML + XSD "required"?

Posted on 2004-04-08
4
547 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_
  • 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 50 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

808 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