What Can I Do with XML?


Now that you have an understanding of what XML is, let's take a look at how XML might be used in a business application. Let's say you were just hired by a company that manufactures computer parts , and you have several high-profile customers. One of your first responsibilities will be to produce and distribute an electronic version of your company's catalog to your clients every month. The current situation is that each client has a specific requirement for the format of the information in the catalog. Luckily, you're very forward-thinking and see this as a great opportunity to start using XML because you can easily separate the data from the presentation. That's a great idea, but how do you get started? A few steps are involved in trying to start using XML for your catalog distribution, so let's take a look at the process.

Step 1: Gather Requirements and Data

First, meet with each of the clients and compile a list of all the information required for the catalog and then the subset of the catalog (if applicable ) for each client. It may be possible to build a master catalog, and then each client's catalog could be a customized report based in the catalog.

After meeting with the clients, you realize that they'll all need the following information for each manufactured item: part name , part number, description, technical specifications, and pricing information.

Step 2: Describe the Format of the Data To Be Stored

Now that you have the list of the required data, you will need to work with the clients and describe the format of the data. The format includes ordering of the data, identifying required versus optional data, and valid values (such as enumerated lists or data types). This will assist you in verifying that the required data is present and in the proper format.

Step 3: Design a DTD or an XML Schema

Formalize the structure and contents of the XML document by developing a DTD or an XML schema. Both DTDs and XML schemas specify the structure and content of an XML document. However, there are differences between DTDs and XML schemas.

Typically, DTDs are easier to develop because their syntax is simpler. However, that ease of use doesn't come without a price. For example, DTDs don't differentiate between data types. A validating XML parser can look at an XML document and the corresponding DTD and verify that a particular element is present, but it cannot verify that the element has the correct data type (such as text versus an integer). Also, DTDs are not valid XML documents, so they cannot be parsed by standard XML parsers.

XML schemas are more difficult to develop. One reason is that you must define a data type for each element. However, this extra work is rewarded with several important benefits. First, XML schemas are XML documents and can be processed by a standard XML parser. Second, XML schemas are much stricter regarding format and data type. For example, in addition to verifying that data is present in a particular element, an XML schema can also verify that the data is the proper type (such as Boolean, float, or string), how many times a particular element can appear, and whether the element value is within the expected range (for example, between 1 and 10). As you can see, a well-defined XML schema can perform nearly all your required input data validation.

Note

Think of a DTD or a XML schema as a contract ”after both sides agree, it cannot be changed without additional negotiation.


At the start of a new project, be sure to spend enough time, and don't rush designing the DTD or XML schema. Try to anticipate any future growth by adding placeholders for data that may be required. Depending on the complexity of the DTD or XML schema, it may be time consuming to modify applications that are dependent on the DTD or XML schema. After you build an application or series of applications based on a particular DTD or XML schema, changes in the DTD or XML schema can sometimes trickle down to additional components in the architecture. For example, let's say that you have an application that parses XML data and stores the data in a database. When either the DTD or XML schema changes, both the parser and the database schema must be modified. If this is a large project, these changes probably won't be trivial. This isn't really an XML issue, it is a basic configuration management issue, but try to localize changes whenever possible.

Step 4: Exchange and Process XML Documents

Develop and start exchanging XML documents between the manufacturer and the clients. Sounds easy, right? Well, there are a few remaining tasks that must be completed before this can occur.

If you are distributing the documents, you will need to develop an application that generates well-formed and valid XML documents. A well- formed XML document is one that conforms to the XML syntax rules developed by the W3C ”this way, it is a legitimate XML file. A valid XML document is one that conforms to your relevant DTD or XML schema. XML documents may contain a lot of data, and trying to generate a large XML document by hand would be tedious and error-prone . Also, XML documents can be generated from a variety of sources ”web-based forms, database tables, Comma Separated Values (CSV) files, spreadsheets, and so forth.

If you are on the receiving side of an XML document, you will need to have an application to parse the XML document. Then, after the document is parsed, what do you do with the data? The answer is, just about anything you want ”generate another XML document, store the results in a database, or even dynamically generate HTML code to display the XML document (or a subset of the XML document) on a web page. Luckily for us, Perl has a suite of XML-based modules that will perform these tasks (and many others).

Learning More About XML

XML has many mailing lists, newsgroups, and support web sites. I recommend searching and joining some of them because there is no better way to learn about a technology than to listen to the everyday questions and discussions that are raised on these lists, groups, and web sites.

You can find an index of XML mailing lists at http://www.oasis- open .org/cover/lists.html.

Because XML is a W3C standard, I strongly suggest that you take a look at http://www.w3.org for XML and related standards documentation and resources.

Before I talk about processing XML documents with Perl, let's take a quick look at Perl.



XML and Perl
XML and Perl
ISBN: 0735712891
EAN: 2147483647
Year: 2002
Pages: 145

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net