Featured ProjectsPress Releases |
XML and XSD (Schema) PrimerI decided to put this little XML tutorial / primer together for those of you who are interested in learning XML, but don't want to spend weeks doing so. This 15-minute course should give you guys a good basic understanding of how XML works without much time investment. I suggest that if you find this primer interesting, go read up more on XML (and the associative standards) and learn more. There's a lot more you can do with it than what I'm showing here. XML (Extensible Markup Language)There's not much to XML, other than some basic rules. Here is a summary:
XSD (XML Schema Definition)XSD is a very simple markup language that is used to define an XML standard (schema). It looks a lot like HTML, but with different elements. Here are some of the basic elements of a schema: schemaThis is the root element of the schema (like the "html" element in an HTML document). The schema must contain an xmlns Namespace URI pointing to the schema that defines how XSD must be structured. It should also contain a target namespace, which is the uri for the standard that we're creating in this schema. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" The prefix on the elements (xsd) is a reference to the xmlns defined in the schema. elementAn "element" is used to define an tag that will be allowed in the XML standard you are creating. For instance, if I want to create a standard for CDs, I could create an element called "cd" like so: <xsd:element name="cd" type="xsd:string" /> This definition will allow me to create an element like this: <cd>Eminem</cd> attributeThe "attribute" element is used to define an attribute. Attributes must be defined within the context of a complexType (see below). For instance, if I wanted to give my cd element above attributes like "title" and "artist", I could create the following: <xsd:element name="cd"> Note that the "type" attribute was removed from the element tag and a new "complexType" element was added within the context of the element, containing the desired attributes. Here's what the output might look like: <cd name="The Eminem Show" artist="Eminem"> simpleTypeThe "simpleType" element is used to define simple types that extend or restrict the basic XSD types. Basic XSD types are string, integer, decimal, datetime, etc. Here are some simpleType restrictions: minLength / maxLength - sets the minimum or maximum length of a restricted simpleType. Here's an extension of the example above to make the base string restricted to only allow strings of 9 characters. <xsd:simpleType name="ssnType"> In the case of integers, you can also use the minInclusive / maxInclusive elements, which limit the numeric value contained in the integer. patternPatterns allow definition of a regular expression pattern to limit the allowed content of a restricted type. For instance, the following pattern will match any string consisting only of letters and numbers. <xsd:simpleType name="serialNumberType"> enumerationEnumerations allow you to define a list of possible values allowed for a simpleType. Here's an example: <xsd:simpleType name="genreType"> complexTypeThe "complexType" element is used to define complex types, which are composed of one or more attributes and elements put together in sequences or choice lists. An example of a complexType element would be an HTML <table> type. It contains attributes (cellpadding, etc.) and child elements (<tr>, etc.). Here's an example of a few different ways of composing complexTypes: sequenceA "sequence" specifies a list of elements that may occur in a complexType. They must all occur (unless specified with a minOccurs value of 0) in the order arranged in the sequence. <element name="person" type="personType" /> The resulting schema would allow something like this: <person> Notice in the XSD example above that I'm inheriting the named complexType in the "type" attribute of the definition for the "person" element. This is a good habit to get into for defining data types that will be used by many elements. allAn "all" element is similar to a "sequence", except that it doesn't require that the child elements be arranged in any specific manner. For instance, in the example above, <lastName> could be placed before <firstName> if an <xsd:all> were used in place of the <xsd:sequence> anyAn "any" element is similar to an "all" element, except that it doesn't require that all child elements be used. For instance, in the example above, the <person> could have only a <firstName> specified if <xsd:any> were used. choiceA "choice" element is similar to an any element, except that it requires that only one child element be used. For instance, in the example above, the resulting <person> element could only have one of <firstName>, <middleInitial>, or <lastName> specified. attributesIf you're going to define attributes in a complexType, you must place them after the sequence, all, any, or choice element. ExampleHere's a complete example of XSD and XML in action. I decided to go with the CD example that I started above. <?xml version="1.0"?> And here's an example of a resulting XML document:
|
Design and content copyright Transio, LLC © 2007-2009. All rights reserved. Any and all reproduction without express written consent is strictly forbidden.
Transio
•
Software Development Miami
•
Web Design Miami
•
Web Marketing & SEO Miami
•
Serving Miami-Dade, Broward, and Palm Beach.