Home > Articles > Web Services > XML

This chapter is from the book

Classifying Attributes: Attribute Types

Attributes can be classified based on the kind of information that they contain. When you define an attribute in XML, you are really defining the attribute type, or the kind of information that the attribute may legally contain to describe an element. Attributes can be one of three different types: strings, tokenized types, or enumerations.

In the interest of compatibility and to create rules for validation (which we will discuss later in this chapter), there are a number of different types of attributes. Attributes consist of predefined datatypes that can be assigned to your attributes when you are working with a Document Type Definition or an XML Schema.

For example, one attribute type is ID, which is an identifier for the element, a unique identifier. What this allows you to do is to refer to an element by its ID, rather than the tag name. An ID attribute can be used to provide a unique identifier for a specific instance of an element, whereas the element name might refer to multiple instances of an element within a single document. If you are working with a DTD or an XML Schema, you can declare your attributes to be an ID type.

Why is this useful? We could have a document describing the parts of a camera:

<camera>
<part ID="body">Nikon F4</part>
<part ID="lens">28mm</part>
<part ID="flash">Speedflash 50</part>
</camera>

Each of the part elements refers to a part of the camera, but what if we wanted to refer to just the lens for the camera? We could not refer to the part element, because there are several parts to the camera. Instead, with the ID attribute, we can reference the appropriate part by ID. This becomes even more useful for documents with large amounts of similar data, such as a catalog:

<catalog>
<part number="09339">Hyrdolic Pump</part>
<part number="33881">Flange Gasket</part>
<part number="33291">Flexi-hose</part>
</catalog>

TIP

Because you can have attributes of different types, sometimes you might want to have the type of the attribute explicitly in the name of the type. For example, we could have named our attribute "number_ID" to indicate that the attribute was a number. If you are working with attributes that are just text (CData), this probably isn't necessary, but for IDs or IDREFs it can come in handy.

Here we have a listing of parts that might not even be related to one another, other than as part of the same catalog. Although the usefulness of this type of attribute is readily apparent, the value of some other attribute types might not seem so straightforward.

NOTE

Attribute names do not necessarily reflect the datatype of the attribute. For example, the number attribute used in the catalog example could still be an ID attribute, even if it is not called ID.

Attribute types are associated with a specific datatype only if you are working with valid XML, including a DTD or an XML Schema. Otherwise, XML parsers do not differentiate between the types of information in attribute content.

Taking advantage of most of the different types of attributes requires that you use a DTD or XML Schema with your document. However, you can still use attributes in an XML document that is only well-formed. We'll talk more about using attributes with DTD later in Chapter 4 and with XML Schemas in Chapter 5. However, now let's get acquainted with the different types of attributes that you will have at your disposal, so you can start thinking about them as you work with XML.

ID

The first type, and arguably one of the most useful, is the ID attribute type. You've already seen an example of how an ID attribute can be used to help classify parts. The basic idea of an ID is to provide a unique identifier for each instance of an element type in a document.

This is important, because the nature of classifying information leads to multiple instances of elements. If you had an XML document describing the holdings of a library, you would probably have a book element, and there would likely be many instances of the book element. There might be hundreds, if not thousands, of <book> entries in your document. Although you might have child elements that would help narrow your searches, such as <title> or <author>, you could use an ID attribute to provide a unique identifier, that could then be referenced by other attributes or elements. The unique ID attribute might be the call number of the book, for example, which would allow you to quickly locate the correct book element that you were looking for.

This is a very important concept as XML usage becomes ubiquitous. Providing unique, internal identifiers can be a great mechanism for linking to specific elements within a document.

There aren't too many rules to follow when creating ID attributes:

  • The attribute name has to be a valid name. That is, like element names, it cannot begin with xml and so on.

  • The value of each attribute with an ID type must be unique.

IDREF and IDREFS

The next attribute type we're going to discuss goes hand-in-hand with the ID attribute type: IDREF. An IDREF attribute is a reference to an ID attribute. IDREFS refers to more than one IDREF.

What good is a reference to an ID? Well, for starters it can be a great way to organize and classify information. Let's look at an example. Suppose we work for a company that sells cameras and lenses. We want to have a document that lists the parts of each item we sell, which might look like this:

<catalog>
 <product type="film-camera">
 <part>Nikon F4 Body</part>
 <part>28mm Lens</part>
 </product>
 <product type="digital-camera">
 <part>Nikon D1 Body</part>
 <part>28mm Lens</part>
 </product>
</catalog>

Here we have two products, with an ID attribute called TYPE which lets us differentiate between our different types of cameras.

However, in this example, each camera shares a common part, the 28mm Lens. The lens in question is actually the same part, but we might sometimes need to be specific with respect to which camera the lens in question belongs. For example, we might want to alter the filter that shipped with the lens depending on whether it was for the film or the digital version of the camera.

There are several ways we could do this. We could create new elements such as <digital-lens> and <film-lens>; however, it really is the same lens, so instead, we can create an ID and an IDREF to link the two elements together. Our modified document might look like this:

<catalog>
 <product name="F4" type="film-camera">
 <part model="F4">Nikon F4 Body</part>
 <part model="F4">28mm Lens</part>
 </product>
 <product name="D1"type="digital-camera">
 <part model="D1">Nikon D1 Body</part>
 <part model="D1">28mm Lens</part>
 </product>
</catalog>

Now each of our elements has an ID attribute called name that is used to uniquely identify each product. We then can use an IDREF attribute—in this case, model—to refer to the name attribute of the appropriate product. Keep in mind that these types would all be declared in our DTD or XML Schema. Being able to refer to the specific product by a unique name can help ensure that future items that need to be linked to a model can also be linked correctly—for example, if we added a section for filters, we have the model number associated with the lens, so we could ship filters based on the model of camera.

Although, in this case, the relationship is clear because of the document structure, we might have a separate file that looks like this:

<catalog>
 <product model="F4" type="lens">28mm</product>
 <product model="F4" type="lens">35mm</product>
 <product model="F4" type="lens">50mm</product>
 <product model="F4" type="lens">70mm</product>
 <product model="D1" type="lens">28mm</product>
 <product model="D1" type="lens">35mm</product>
 <product model="D1" type="lens">50mm</product>
 <product model="D1" type="lens">70mm</product>
</catalog>

Here, we still have references to the ID attributes, even though the relationship of the lens is not clear from the structure. In fact, we can group together elements to many different IDs, by using the attribute type IDREFS, which is just more than one IDREF:

<catalog>
 <product model="F4 D1" type="lens">28mm</product>
 <product model="F4 D1" type="lens">35mm</product>
 <product model="F4 D1" type="lens">50mm</product>
 <product model="F4 D1" type="lens">70mm</product>
</catalog>

Now you will notice that our attribute for the shared lens reads <product model="F4 D1">. You can use the IDREFS type to reference multiple IDs, each one separated by a space.

To make effective use of these datatypes for attributes, you will need to use a DTD. The reason for this is simply because without a DTD, all attributes look the same to a parser. There is no way for an XML parser to know whether the attribute is an ID or not unless that datatype is specified in the DTD. We'll talk more about how you define attributes in the DTD later in Chapter 4.

Enumerated Attributes

One of the most useful types of attributes that can be defined in XML is enumerated attributes. These attributes allow you to define a list of values for the attribute, and allow users to select the value from that list. It's a way you can create multiple-choice attributes.

This is useful for a number of different applications. For example, let's say that you were defining an XML document for a clothing catalog. You might have XML that looked something like this:

<catalog>
 <item>
 <shirt>Men's T-Shirt</shirt>
 </item>
 <item>
 <shirt>Women's Blouse</shirt>
 </item>
</catalog>

It would certainly make sense to have an option for specifying the size of the garments that are available. One way might be to offer a choice from a list of available sizes—for example, Small, Medium, Large, and Extra-Large.

We can add a size attribute to the <shirt> element:

<shirt size="Large">Women's Blouse</shirt>

This attribute allows us to specify the size of an item. However, as it is currently defined, the value of the size attribute could be literally any value. To limit the choice of the value to "Small," "Medium," "Large," or "Extra-Large," we need to make the attribute an enumerated type.

An attribute type of enumeration means that the value for the attribute must be chosen from a list of attribute values defined in the DTD or XML Schema. Just like other attribute values, the enumerated values have to meet the constraints for attribute content (that is, no < symbols). And you can't use enumerated attributes without a DTD or an XML Schema, because without the rule specifying what the choices are, there is no way for the XML application to know ahead of time what you want them to be.

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