Presents your XML E-NEWSLETTER for December 4, 2002 <-------------------------------------------> REUSE ATTRIBUTE DEFINITIONS WITH ATTRIBUTEGROUPS When creating schemas for your XML documents, you often use attributes to define metadata about particular elements. Sometimes many elements share the same attributes. Did you know that you could create an attribute group and reuse it rather than supplying each attribute for each element? ONE COMMON APPROACH Let's illustrate this concept with an example. Imagine a document that is used to specify a layout for an article. To keep things simple, our layout only includes headers and paragraphs. LISTING 1 shows what our XML document looks like: Listing 1: layout.xml
Learning about XML
Example documents are a good place to start learning XML. By examining specific applications of XML, you can quickly understand how it's best applied. You can find examples of XML all over the Web.
XML Resource
It's always helpful to have a reference. For XML, the ultimate reference is the W3C's XML specification, which details all of the features. You may also want to pick up a good book or two.
So far, our document doesn't include any attributes for its elements. However, when we render this layout, there are some specific fonts and font attributes we want applied to each section. To handle these requirements, our layout document is really going to look like LISTING 2: Listing 2: layout-revised.xml
Learning about XML
Example documents are a good place to start learning XML. By examining specific applications of XML, you can quickly understand how it's best applied. You can find examples of XML all over the Web.
XML Resource
It's always helpful to have a reference. For XML, the ultimate reference is the W3C's XML specification, which details all of the features. You may also want to pick up a good book or two.
SCHEMING ON SCHEMAS Now that we understand what the underlying document looks like, we're going to create a schema that defines the rules for all of our layout documents. Our first stab at creating the schema probably looks a lot like LISTING 3: Listing 3: layout.xsd As you can see, we've used the same attributes for both the header and paragraph elements. What if we decide to add a new attribute, or change the name of an existing attribute? We would have to change it in multiple places. That's not a big deal with this document, but with larger documents, it could be. In addition, there's an easier way to implement the same attributes for multiple elements. THE ATTRIBUTEGROUP ELEMENT Within the XML Schema definitions, there is an underused element called attributeGroup. This element allows you to define a set of attributes that function as a coherent group, which you can apply to multiple elements. The syntax is straightforward--you define the group by listing multiple attribute elements within an attributeGroup element, like this: Once you define the group, you can apply it to a particular element like this: It's interesting to note that both the definition of attribute group and the reference to a particular attribute group use the same attributeGroup element. This is a common theme in XML Schemas. PUTTING IT TOGETHER Now that we understand what attribute groups are, we can apply them to the schema for our layout document. As you can see in LISTING 4, we now define the attribute group and then reference it for both the header and paragraph elements. Any modifications to the attributes can be made in the group rather than at each instance of it. Listing 4: layout2.xsd Brian Schaffner is a senior consultant for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Telcom360 group. ----------------------------------------