Creating an XML Document

 < Day Day Up > 



Now that you know what to expect from Word's XML features, you're ready to work through the process. Start your XML document by following these steps:

  1. Display the File menu, and then choose New.

  2. In the New Document task pane, click XML Document, as shown in Figure 29-4.

    click to expand
    Figure 29-4: When you create a new document, Word 2003 gives you the option of creating an XML document.

Attaching a Schema

When the XML Structure task pane appears, click the Templates And Add-Ins link. The Templates And Add-Ins dialog box (shown in Figure 29-5) appears, where you can select the schema you want to use. The first time you attach a schema, however, the list will be blank.

click to expand
Figure 29-5: The XML Schema task pane includes tools for attaching, working with, and validating XML files.

To add a schema for the first time, follow these steps:

  1. Click Add Schema. The Add Schema dialog box (shown in Figure 29-6) appears, where you can select the schema you want to use.

    click to expand
    Figure 29-6: Select the schema you want to attach to your XML document in the Add Schema dialog box.

  2. Click the schema, and then click Open. In the Schema Settings dialog box, enter a name under which the schema will be listed in the XML Schema tab (known as the namespace alias), and then click OK. The schema is added to the list and you can select it and then click OK to complete the process.

    Note 

    This section assumes that you have a schema to work with. If you have not been supplied an XML schema by your administrator or workgroup leader, you can search online for sample schemas to use or follow along with the XML examples in the book XML Step by Step, Second Edition, as mentioned earlier. The book includes a CD with numerous XML sample data files, schemas, and transforms you can use to learn more about XML.

    Note 

    You also can attach a schema by choosing Tools, Templates And Add-Ins, and then clicking the XML Schema tab.

start sidebar
An Overview of the Schema Library

The Schema Library, shown below, contains any XML schemas and XSLT (Extensible Stylesheet Language Transformation) files you will use to control the structure and format of your XML documents. To display the Schema Library, choose Tools, Templates And Add-Ins, and then click the XML Schemas tab. Select Schema Library.

click to expand

The top portion of the Schema Library lists the XML schemas you've attached to the current document. The bottom portion of the dialog box enables you to add XSLT transforms, also called solutions, to the document. To add a solution, click Add Solution, navigate to the folder in which the transform is found, and then choose the XSLT transform from the list that appears. Click OK to add the transform and return to the Schema Library.

end sidebar

Working with Tags

Most of your work with XML will rely on working with tags—the small code identifiers you attach to data to name the content. The tags available for use in a particular document depend on the schema attached to it—the schema contains the definitions for the tags, much as a style sheet defines the styles that are allowed in a document formatted for the Web.

Displaying Tags

You can easily show and hide XML tags while you work by pressing Ctrl+Shift+X. If you prefer, when the XML Structure task pane is visible, you can click Show XML Tags In The Document to remove the check mark and hide the tags.

In addition, to make finding the tag you want easier, you can choose which tags appear in the selection box in the bottom of the task pane by clicking the List Only Child Elements Of Current Element check box.

Adding Tags

Once you've attached the schema to the document, the next major step is to tag the content you want to use. To add XML tags to data in your current document, follow these steps:

  1. Highlight the data you want to tag.

  2. Click an XML element in the XML Structure task pane. The opening and closing tags for the item are added to your document, as shown in Figure 29-7.

    click to expand
    Figure 29-7: You can add XML tags easily by highlighting information in your document and clicking the tag you want to apply.

Adding Tag Attributes

XML attributes are special characteristics of an element that allow you to further categorize your data. For example, suppose that the training classes in the previous figure are offered to both groups and individuals. To help track the data on each type of training class independently, the CLASS tag could be assigned GROUP and INDIVIDUAL attributes.

To add an attribute, perform the following steps:

  1. Right-click the XML element in the XML Structure task pane, and then click Attributes in the list, as shown below.

  2. The Attributes dialog box appears. Click the attribute you want to use, enter an attribute value in the Value text box, click Add, and then click OK.

  3. The new attribute is added to the list of available attributes. Now select the one you want and then click OK to add it to the XML element you selected.

Removing XML Tags

If you make a mistake entering a tag in your document and you want to remove it, simply right-click the tag you want to remove, and then choose Remove Tag. The data is still in your document but both the opening and closing tags are deleted.

Tip 

If you want to print your document complete with its XML tags, choose Tools, Options, and click the Print tab. Click the XML Tags check box in the Include With Document section, and then click OK to close the dialog box. Now print as usual (choose File, Print) or click the Print button on the Standard toolbar) and the XML tags will be printed in your document.

Setting XML Options

As you get more comfortable working with XML, you may want to modify some of the options to further control the way your XML files are saved, validated, and displayed. To display the XML Options dialog box, click the XML Options link at the bottom of the XML Structure task pane. The XML Options dialog box appears, as shown in Figure 29-8.

click to expand
Figure 29-8: XML options enable you to control the way XML files are saved and to make choices about validation and display.

Saving XML

When you save your XML document (as you'll see more fully in "Saving XML Files"), you have the option of saving only XML data or saving the data and the format together. In the XML Options dialog box, you can make one of these settings the default choice for saving the current document. If you click the Save Data Only check box, the current XML document will be saved as data only. (You'll have the option to override this choice in the Save As dialog box, however.)

When you click the Apply Custom Transform check box, the Custom Transform field becomes available so that you can click Browse, and then choose the transform file you want to attach to the document.

Dealing with Schema Violations

If the tags you've added to your document—or the content they describe—create something that doesn't fit the schema you've attached, Word will let you know that you have a validation error. Validation is the process of checking the tags against the schema to make sure the code is properly formed. If Word finds a problem, it lets you know by displaying a purple wavy line along the left margin of the text, as shown here:

In addition to the purple indicator, a small error icon appears in the XML Structure task pane next to the tag that is in error. To see a description of the problem, position the mouse pointer over the tag. A description of the problem appears, as shown in Figure 29-9.

click to expand
Figure 29-9: Word lets you know when an error occurs in your XML document.

The Schema Validation Options in the XML Options dialog box enable you to control how—and whether—Word checks your code for validation (this option is also available in the Templates And Add-Ins dialog box, where you first attach the schema). By default, the document will be validated against attached schemas. You can choose to hide errors in the current document, ignore mixed content, or even save the file as XML, errors and all. (You might want to do this, for example, if you have an error you can't figure out and you want to give the file to an XML guru for further investigation.)

View Options for XML

The XML View Options in the XML Options dialog box enable you to provide additional information about the XML elements you're working with. By default, the namespace alias (the name you assign to the schema or solution when you attach it) appears in the XML Structure task pane; you can hide it by selecting the Hide Namespace Alias In XML Structure Task Pane check box. If you want to get additional information on the XML validation error messages you receive, select the Show Advanced XML Error Messages check box. Finally, if you want to see where empty elements occur in your document, select the last option, Show Placeholder Text for All Empty Elements.

If you are just learning how to use XML, you'll find that selecting the Show Advanced XML Error Messages feature will help you learn more quickly from your mistakes. The error messages that appear provide much more information about what's going on. For example, compare Figure 29-10 with the earlier, more limited error message shown in Figure 29-9.


Figure 29-10: When the Show Advanced XML Error Messages option is checked, Word provides more explanation of validation problems.



 < Day Day Up > 



Microsoft Office Word 2003 Inside Out
Microsoft Office Word 2003 Inside Out (Bpg-Inside Out)
ISBN: 0735615152
EAN: 2147483647
Year: 2005
Pages: 373

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