MAPPING ATTRIBUTES
Mapping Excel data to XML attributes
is slightly more complicated than
mapping elements. There are also
differences depending on whether you
are attempting to map an element that
contains both an attribute and text,
see Attribute Example A, or whether
you are trying to map and element that
contains only an attribute or an
attribute and elements, but no text,
such as Attribute Example B.
First, we will go over how to map
attributes as shown in Example A,
where the elements contain both
content and attributes. In order to do this, we will make use of two new XSD elements, simpleContent and
extension. SimpleContent is used when an elements contains only text or attributes. Extension is used in
conjunction with simpleContent to define the content allowed in the element. (“XSD Text-Only Elements”)
In our example, we will add a column for
the scientific name of our fruits, as shown
on the right. Rather than include this data
as a new element, we are going to include
it as an attribute of our name element.
Then we will extend the content of our
name element in our XSD as shown in the
screenshot below. “Name” now becomes
a complex element, like “fruits” and “fruit.” This means that the tag is no longer self-closing. The element also
cannot declare a type of textual content and contain an attribute, so the “name” element will now look like this:
<xs:element name=”name”> Do not forget to close this tag with </xs:element> as was done with “fruit” and
“fruits.”
Within the name element, add the tag <xs:complexType> to declare that this element contains other items.
However, because this element only contains text and an attribute, we use the tag <xs:simpleContent> rather than
the <xs:sequence> tags used in “fruit” and “fruits.” Next, add the tag <xs:extension base=”xs:string”>. This tag
extends our “name” element to allow for textual content. Note that this tag is not self-closing. The tag declaring
the attribute is located within it. That tag is named <xs:attribute>. It is formatted the same as tags declaring
elements, with an attribute for the name of the attribute and another attribute for the type of the content.