Join Jim Maivald for an in-depth discussion in this video Using Find and Replace to modify XML, part of InDesign CS5: Dynamic Publishing Workflows in XML.
Many times you receive XML that you can't use. The tag names are incorrect, they are out of order, the existing structure doesn't conform to your workflow. There are many ways to edit the XML to make it work for specific workflow. In this exercise, we're going to show you a simple way to fix your XML file using Find and Replace. Although Find and Replace is not as powerful as an XSLT, it's fast, it's easy, and you don't need any special training to get it done. Let's take a look at this file. It contains the markup for our staff ID badges. I'm highlighting an XML element that automatically brings in the photo to our layout.
You may notice that this element is different than some that we've already examined. For one thing, here we see the opening tag, but I don't see the closing tag. You can see this slash and closing bracket here, but there is no tag name. This is called an empty tag in XML. There is no data inside the tag. Just an attribute. The attribute is actually the pathname for our photo. InDesign treats this just like one of the links in the Links panel. Import this XML file, and the graphic will appear in the layout too. But this was not the way the element was sent to us.
Here is the actual XML file exported from the database. You can see that the staff_photo element is a normal element with an opening tag and a closing tag. If we imported this file into InDesign, we would end up with just the word jmartin.jpg in our layout. This would not bring in the photo. Most databases can't create a photo link on export. Instead, the name of the file is added to the XML in this way. We could change this structure with an XSLT, but that's later. The change that we need here is so simple that it can easily be done with Find and Replace.
Let's show you how. The first thing is to understand how to structure an empty element properly. Let's take a closer look at the one that's correct. The first thing we see is the opening element, staff_photo. Then we see the HTML attribute href, which stands for hypertext reference. The text file://photos indicates the path on the hard drive where we can find the jmartin.jpg file. You'll notice that this path is not identifying the hard drive or the network drive that this file is located on.
This is considered a relative link. For this reference to work in InDesign, this XML file must be inserted at the root containing the photos folder. So our first step is to convert the staff_photo element into a staff_photo element with a href attribute. Let's show you how to do it. To build the reference properly, we need to search for the opening element. So I'm going to go ahead and select that entire element and copy it into memory. Now go ahead and select the Find and Replace command. Go ahead paste the opening element.
Insert our cursor in the Replace field and paste the element again. By using only the opening tag, Notepad will ignore all other tags in your layout. Go ahead and delete the closing bracket. Insert a space and type href. Insert an Equal sign. Remember, attributes in XML must have quotation marks. Type the word file://photos. In this case, the name photos is the folder in which the images reside. Don't forget to put the second slash. When I use Find and Replace, I like to do at least one test before I click Replace All to see everything is going to work properly.
So I'm going to go ahead and click Replace. Notepad will automatically jump down to the next occurrence of that element. Go ahead and check the first change to make sure it's correct. staff_photo href="file://photos/. It looks like everything is fine. Let's go ahead and click Replace All. Notepad is a basic text editor. It has no extra features. It's plain Jane. It doesn't give me any productivity help at all. This capability is offered in almost every text editor that's on the market today. Feel free to use any text editor that you feel comfortable with.
Now let's create the proper ending of our element. An image reference is an empty element. To create the proper structure, we need to get rid of this ending tag. Then swipe over it and copy it. Because we are including the entire tag, including the slash, and pasting it into the Find field, this will search and find only the closing tags for the staff_photo element. Go ahead and swipe over all the text in the Replace field. Don't forget to add the quotation mark at the end of the attribute. Type a space, type a slash, and then type a closing bracket.
Again, as before, we'll click Replace to see if it works properly. It looks like our element has worked. Let's go ahead and do Replace All. Be sure to scan down through the file to make sure that everything worked properly. When you're done, save the file and you're ready to use it in an InDesign workflow. Find and Replace can also be used to create completely new structures in your XML file. You'll notice that the employee name is made up of two elements, the first name element and the last name element.
In some workflows, a first name and last name will be inserted into a parent element. You can use Find and change to create such a structure. Let's show you how. As before, I'm going to go ahead and swipe over and select the first name element. I'm going to copy it. For this technique to work properly, this element can only appear in the layout in this manner. Now I'm going to go ahead and paste the new element. I'm going to go into the Replace field and paste it again. Let's say we want to add a parent element called staff_name.
Go ahead and insert it at the beginning of the Replace field. Click Replace. Check your work. Staff name, staff first name. Go ahead and click Replace All. This first operation has inserted the opening tag of the staff_name element. If you already guessed how we're going to create the ending, you're right. Let's select the staff_lname element, copy that, in the Find what area paste it. In the Replace with, I'm going to actually insert it in front of staff_name and paste.
Now I want to use the name staff_name as the closing tag, but it's not formed properly. It needs a slash. Well, it's a simple matter of inserting your cursor and typing the slash. But before you click Find and Replace, make sure you delete the staff first name element from the Replace field. Let's go ahead and try it now. Check your work. Sure enough, we've closed the last name, and we've close the parent element. Go ahead and click Replace All. Now as you can see, using Find and Replace is an easy way to get your XML to conform to your workflow.
- 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