information. Database managers such as Access always use a schema (you
define it when you create a table), but other Office applications don’t
because you normally don’t need to organize the information by using a
schema. Figure 11-4 shows a typical worksheet. The data structure is proba-
bly obvious to you because each of the columns is named, but the computer
doesn’t have a clue to what that data means.
An
XML Schema Definition
(XSD) file contains a description of the information
that you want to export from or import to an Office application. It doesn’t
matter which application you use because, at some point, you have to create
an XSD file to perform pure XML data transfers.
Listing 11-1 shows an example
of an XSD file that describes the data shown in Figure 11-4. (You can find the
source code for this example on the Dummies.com site at
http://www.
dummies.com/go/vbafd5e
.)
Listing 11-1
Creating an XSD Description
Figure 11-4:
Understand-
ing data
formats is
easy for you
but hard
for the
computer.
244
Part III: Expanding Your VBA Horizons
17_046500 ch11.qxp 12/5/06 5:37 PM Page 244
This is yet another XML file, so it begins with the standard XML processing
instruction. The second line contains the URL for the XSD file definitions. You
must include this line in all your XSD files. You can include other information,
such as a personal namespace,
as part of this header, but it’s all optional. See
the tutorial at
http://www.w3schools.com/schema/default.asp
for
additional information.
The structure of your information comes next. Chapter 9 describes how VBA
collections work. The tags that XSD relies on to define your data act much
like a collection. The top element,
Items
, defines the collection name. The
tag tells the XSD parser that you’re defining something
other than a string or an integer. You use the
tag to tell the
XSD parser that the next set of elements must appear in the order shown and
that the parser can’t skip any of the elements.
The
tag shows
how to create an individual item within an XSD collection. The two things
that set this tag apart from the
tag is that it appears within the
tag and also uses the special
maxOccurs
attribute. Notice that the
maxOccurs
attribute is defined as
unbounded
(unlimited) so that the
Items
collection can contain as many individual
Item
elements as needed.
The
-
tag is also a complex type and requires elements in a specific
sequence. This time, the individual elements are simple types. The
tag is a string, but the
and
tags are both in decimal nota-
tion. XSD doesn’t currently support a currency type, so your best choice is to
use a decimal to ensure that the XSD parser preserves the accuracy of the
data that you transfer.
Defining XSD to worksheet linkage
Writing the XSD file creates a definition that you can use to work with data in
Excel. However, you still haven’t created linkage between the XSD file and the
Excel document. (You should perform this task by using a document tem-
plate in Word — see the upcoming “Creating a Simple Word XML Document”
section for details.) Here’s how to create linkage in Excel 2007:
Do'stlaringiz bilan baham: