Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Should XSD Files Be Used?

Tags:

xml

xsd

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:

  1. When do you need an XSD? Why would you use one?
  2. 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.

Also, is it possible to describe functions in an XSD/XML document?

Thanks ahead of time.

like image 205
Eric Avatar asked Jun 26 '11 22:06

Eric


2 Answers

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.

like image 138
Michael Kay Avatar answered Sep 28 '22 07:09

Michael Kay


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.

like image 23
d-live Avatar answered Sep 28 '22 06:09

d-live