Solved

XML + XSD "required"?

Posted on 2004-04-08
4
546 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to ToolkitScriptManager 2 61
Problem to Popup 2 83
Windows and other UI names - The Proper names for the Various elements! 1 85
write xml in vb.net 2 24
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 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