Join Jim Maivald for an in-depth discussion in this video XML creation basics, part of InDesign CS5: Dynamic Publishing Workflows in XML.
The first question everyone has when they work with XML is, where does the XML come from? The answer is just as simple. It can come from a number of sources and workflows. Some people create it by hand, in a text or XML editor; some create it using databases and spreadsheets. Frequently, you'll use XML that will come from a number of sources. In our first hands-on exercise we'll explore how easy it is to create XML using just a plain text editor. In Windows, you can use Notepad. On the Mac you can use TextEdit or BBEdit. I'm going to go ahead and start typing. I'm going to create our root element. Now, the root element can be named anything you want it to be named, as long as it follows the XML naming rules.
Once you create your root element, then you can start creating your parent elements. Your parent elements will contain all the sub-elements within one record. So in this case, we're going to be creating a record for one of our staff employees. I'm going to call it staff. And then we start with their employee ID or staff ID. Now, when you're working with data, it's always a good idea to have a unique identifier for each person. This could be their employee ID, Social Security number, or any other kind of number that you want to add to the file later. This allows you to bring back the data later on in your workflow and pull one or two records out at a specific instance.
I'm just going to go ahead and add an arbitrary number to that. Now, it's important as you close your elements that the elements are closed with the same exact spelling and structure of the opening element. So if you'll notice that staff ID, both in the opening and closing, are spelled the same way, with the same capitalization. If anything is incorrect, your XML element is broken. Let's go ahead and create an element for their first name. Again when you're closing it, make sure you close it with the same spelling. Some people like to use copy and paste to help them create their elements.
So to use copy and paste, it prevents you from making any typos. So I'm going to go ahead and copy that. I'll go ahead and enter some information for the last name. And then when I close it, I'll paste that information back in. Let's go ahead and close this element now. In most XML workflows you'll have more than one record element. This is a good opportunity to use copy and paste to populate this file with multiple records. I'm going to go ahead and copy that employee element and I'm going to paste it. By pasting it several times, I can create three or four employee records very quickly and then go back and change the data to reflect one of our actual employees.
And then go through each record and add unique names and records and then use this information later on for our workflow. Saves you a lot of time from doing all the coding by hand. The exact names are not important. Go ahead and use whatever names are familiar to you. And then finally when you're done, you need to close the root element. There we have it. We have four employees and we did it all by hand. Now that we finished our XML file, we need to save it.
If you're not using an XML editor, you'll have to add the XML extension yourself. Since many of the XML files you're creating may be used in a web workflow, it's a good idea to keep spaces out of the names. So we're going to call this staff, and if you need to put a space in for legibility, go ahead and use an underscore or dash. I'm going to go ahead and add the XML extension. Now, in some programs, when you click Save, the program will prompt you to change the extension to a compatible extension. In this case, Notepad didn't have a problem with it.
- Understanding XML terminology
- Identifying XML elements, attributes, and comments
- Adding, moving, and deleting elements
- Tagging and styling
- Creating tables with XML
- Building frame and flow layouts
- Ordering with XSLT
- Using the Document Type Definition in an XML workflow