Home > Articles > Web Services > XML

This chapter is from the book

Schemas and XML Data Modeling

The process of creating a schema for an XML document is known as data modeling because it involves resolving a class of data into elements and attributes that can be used to describe the data in an XML document. Once a data model (schema) is in place for a particular class of data, you can create structured XML documents that adhere to the model. The real importance of schemas is that they allow XML documents to be validated for accuracy. This simply means that a schema allows an XML developer (or an application) to process a document and see if it adheres to the set of constraints laid out in the schema. If not, you know the document could prove to be problematic. A valid XML document is kind of like a stamp of approval that declares the document suitable for use in an XML application. You learn how to validate your own XML documents in Hour 8, "Validating XML Documents."

To help clarify the role schemas play in XML, let’s consider a practical real-world analogy. Pretend you just met a friend whom you haven’t seen in years and she gives you her email address so that you can get in touch with her later. However, she lists her email address as lucy*stalefishlabs.com. You know that all email addresses consist of a name followed by an "at" symbol (@), followed by a domain name, which means that something is wrong with her email address. The name@domainname format of email addresses is actually a simple schema—you used this schema to "validate" your friend’s email address and determine that it is in error. The obvious fix is to replace the asterisk (*) in her email address with an "at" symbol (@).

You now understand in the simplest of terms how schemas are used to determine the validity of XML documents, but you don’t entirely know why. The main reason schemas are used in XML is to allow machine validation of document structure. In the invalid email example, you were easily able to see a problem because you knew that email addresses can’t have asterisks in them. But how would an email application be able to make this determination? The developer of the application would have to write specific code to make sure that email addresses are structured to follow a given syntax, such as the name and domain name being separated by an "at" symbol. Whereas an email application developer writes code to check the validity of an email address, an XML document creator uses a schema. This schema can then be used by XML applications to ensure that documents are valid; schemas provide a mechanism to facilitate the process of validating XML documents.

When it comes to creating schemas, there are two primary approaches you can take:

  • Document Type Definitions (DTDs)
  • XML Schemas (XSDs)

These two schema approaches represent different technologies that make it possible to describe the data model of an XML-based markup language. The next two sections explain each approach in more detail.

Document Type Definitions (DTDs)

Warning, I’m about to roll out a new acronym! The new acronym I want to introduce you to now is DTD, which stands for Document Type Definition. DTDs represent the original approach of creating a schema for XML documents. I say "original approach" because DTDs did not originate with XML; DTDs originated with XML’s predecessor, SGML (Standard General Markup Language). DTDs made their way into XML because it eased the transition from SGML to XML—many SGML tools existed that could be used for XML. Things have changed since the early days of XML, however, and now there is a more powerful approach to establishing schemas than DTDs. Even so, DTDs are still in use so it’s important for you to understand how they work.

The main drawback to DTDs is that they are based upon a somewhat cryptic language. XML provides a highly structured approach to formatting data, so why should you have to learn an entirely new language to describe XML schemas? I don’t have a good answer to this question except to say that DTDs are a carryover from XML’s beginnings and they still play a role in some XML applications, so you should learn how to use them. The good news is that DTDs are actually quite simple for describing most XML-based markup languages. This is due to the fact that the DTD language is extremely compact, which is why it has a cryptic appearance. Rather than continue to describe DTDs in words, let’s just look at an example in Listing 3.1.

Listing 3.1 A Simple DTD for the Tall Tales XML Document

 1: <!ELEMENT talltales (tt)+>
 2:
 3: <!ELEMENT tt (question, a, b, c)>
 4: <!ATTLIST tt
 5:  answer (a | b | c) #REQUIRED>
 6:
 7: <!ELEMENT question (#PCDATA)>
 8:
 9: <!ELEMENT a (#PCDATA)>
10:
11: <!ELEMENT b (#PCDATA)>
12:
13: <!ELEMENT c (#PCDATA)>

I warned you it was kind of cryptic. However, if you take a moment to read through the DTD code you can actually start to make some sense of it. You might even recognize that this DTD is for the Tall Tales trivia document that you saw in the previous hour. By studying the code, you can see that the word ELEMENT precedes each element that can be used in a TTML (Tall Tales Markup Language) document. Also the attributes for the tt element are listed after the word ATTLIST (line 4); in this case there is only one attribute, answer (line 5). Also notice that the three possible values of the answer attribute (a, b, and c) are listed out beside the attribute (line 5). Although there are a few strange looking pieces of information in this DTD, such as the <! at the beginning of each line and (#PCDATA) following each element, it’s pretty apparent that DTDs aren’t overly complex.

You learn a great deal more about DTDs later in this hour, so I won’t go into more detail just yet. Instead, we’ll move on and learn about the other approach to data modeling that uses a syntax that should be very familiar to you.

XML Schema (XSDs)

XML Schema replaces DTDs with a more powerful and intuitive approach to creating schemas for XML-based markup languages. Schemas created using XML Schema are coded in the XSD (XML Schema Definition) language, and are therefore referred to as XSDs. XML Schema and the XSD language were created by the W3C (World Wide Web Consortium), and represent a considerably more powerful and flexible approach to schemas than DTDs. The idea behind XML Schema is to use XML as the basis for creating schemas. So, instead of using the special DTD language to create a schema, you can use familiar XML elements and attributes that are defined in the XSD language.

An XSD is very similar in purpose to a DTD in that it is used to establish the schema of a class of XML documents. Similar to DTDs, XSDs describe elements and their content models so that documents can be validated. However, XSDs go several steps beyond DTDs by allowing you to associate data types with elements. In a DTD, element content is pretty much limited to text. An XSD is more flexible in that it can set the data type of elements to specific types, such as integer numbers and dates.

Of course, the most compelling aspect of XSDs is the fact that they are based upon an XML vocabulary (XSD). This means that you create an XSD as an XML document. So, the familiar tag/attribute approach to encoding XML documents is all you need to know to code an XSD document. You still have to learn the specific elements and attributes that comprise the XSD language, but it isn’t too terribly difficult to learn. To give you an example, the code in Listing 3.2 is for an XSD that describes the familiar Tall Tales document.

Listing 3.2 An XSD Document That Serves as a Schema for the Tall Tales XML Document

 1: <?xml version="1.0"?>
 2:
 3: <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
 4:
 5: <xsd:element name="talltales" minOccurs="1" maxOccurs="1">
 6:  <xsd:complexType>
 7:   <xsd:element name="tt">
 8:    <xsd:complexType>
 9:     <xsd:sequence>
10:      <xsd:element name="question" type="xsd:string" maxOccurs="1" />
11:      <xsd:element name="a" type="xsd:string" maxOccurs="1" />
12:      <xsd:element name="b" type="xsd:string" maxOccurs="1" />
13:      <xsd:element name="c" type="xsd:string" maxOccurs="1" />
14:     </xsd:sequence>
15:     <xsd:attribute name="answer" type="answerType" use="required" />
16:     <xsd:simpleType name="answerType">
17:      <xsd:restriction base="xsd:NMTOKEN">
18:       <xsd:enumeration value="a" />
19:       <xsd:enumeration value="b" />
20:       <xsd:enumeration value="c" />
21:      </xsd:restriction>
22:     </xsd:simpleType>
23:    </xsd:complexType>
24:   </xsd:element>
25:  </xsd:complexType>
26: </xsd:element>
27:
28: </xsd:schema>

As you can see, XSDs aren’t nearly as compact as DTDs, and can be more difficult to understand initially. The reason for this is because XSDs are considerably more powerful and flexible than DTDs, and with advanced features comes complexity. You learn all about creating XSDs in Hour 7, "Using XML Schema XSDs," after which this code will make complete sense to you. For now, the main thing to understand is that XML Schema allows you to use XML code to model data in a more detailed manner than DTDs. For example, in an XSD you can specify exactly the number of times an element is allowed to appear when nested.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020