Solved

XML + XSD "required"?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now