When Should XSD Files Be Used?
I am having trouble learning the purpose of XSD documents, and would like either an explanation, or a link to a decent resource. I've been reading various websites for a couple hours and haven't found anything that explains the "big picture."
Specifically, I'd like to know:
- When do you need an XSD? Why would you use one?
- What advantages does an XSD offer? What are the alternatives?
From what I understand so far, you basically describe a class structure in an XML format. But what do you do with it at that point? Is it used in during the compilation? Is it used so that you can save class instances in an XML document more easily?
I'm having trouble seeing why you would need to describe a class in an XML file, when you could just use the code file. My first thought was that somehow the XSD gets loaded at runtime, and would allow users to modify it on the fly, but that doesn't really make sense because if an object is added, there is no way to reference it in the rest of the code.开发者_StackOverflow社区
Also, is it possible to describe functions in an XSD/XML document?
Thanks ahead of time.
Your talk of "class files" and "class structure" suggests you are thinking of your data structure as being primarily defined by programming language data structures - that is, you are taking a programming-centric view rather than a data-centric view. That's rather missing the point of XML, which starts from the viewpoint that if applications (especially applications that are independently developed) are to exchange data effectively, then the design of the data needs to be independent of the design of the applications. This "design of the data" is often expressed in a schema (either an XSD schema, or some other kind), and it represents a contract between the communicating applications (just as RFC822 defines the data that an email application must handle). By validating data against the schema, applications can be sure that what they are being sent is conformant with the agreed specification.
XSD allows you to define schema objects. There are many tools out there that take xsd as input and generate classes for those objects. Many of these tools also generate Marshalling/Unmarshalling
- (converting objects to xml and vice versa) code as well in addition to just the plain value object classes. There are many applications that need to convert in-memory object instances to xml representation and reverse and without an xsd and a suitable tool you would need to write the conversion code for each object by hand which can be error prone and/or may be very expensive depending on number of classes in your domain. The xml representatation of an object may not be valid as well if coming from an external source, having an xsd can validate these and let you unmarshal them to object instances.
For further info, see
Castor/Xml Beans etc for java
XSD.exe tool for Microsoft Tech.
Copying from W3Schools:
"
The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.
An XML Schema:
- defines elements that can appear in a document - defines attributes that can appear in a document - defines which elements are child elements - defines the order of child elements - defines the number of child elements - defines whether an element is empty or can include text - defines data types for elements and attributes - defines default and fixed values for elements and attributes
"
In practice it defines the XML that is valid for a given purpose - fro example a system developer can use it to define what consitutes a valid XML file to entry to their system.
You appear to have become confused about one single aspect of XML Schema - their use use as the input to XSD.EXE or some other tool that can do something similar. XSD.EXE consumes an XML Schema file and can produce a set of .NET classes which can be serialized into XML that will validate against the schema.
This is a corner case of a corner case.
XSDs are documents that specify the structure of an XML document and help in their validation. The alternative to these are DTD files. These files are mostly used for html documents.
1) From http://en.wikipedia.org/wiki/XML_Schema_(W3C):
Like all XML schema languages, XSD can be used to express a set of rules to which an XML document must conform in order to be considered 'valid' according to that schema.
Example: We have a XML file layout, which describes an exam. Questions, question types, answers, points, etc. To make sure, every exam file we use in actual exams works as expected, we validate them against a XSD.
2) For a list of alternative (e.g. DTD) schemas and their characteristics, take a look at these slides:
- http://dret.net/lectures/xml-fall08/schemalanguages
精彩评论