The procedures described in this article work best if your organization uses an XML Schema to define data structure. By attaching your Schema to a document, you can use Microsoft Office Word to create a well-formed XML document that consists of your custom XML as well as the formatting and properties that are provided by the Word XML Schema (WordprocessingML).

This use of dual Schemas preserves not only the data as defined by the custom Schema, but also the rich formatting as defined by the WordprocessingML. Alternatively, you can discard the WordprocessingML and save only the content that you tag (custom XML data).What do you want to do?

Attach an XML Schema to a document

The XML Schema that you attach to your document was designed for your organization for a custom purpose. XML Schemas are usually created by IT professionals whose role is to build specialized templates or solutions for your organization in Word.

The Schemas that are available for attaching to your document are listed in the Schema Library.

Add Schemas to the Schema Library

To add Schemas to the Schema Library, do the following:

  1. On the Developer tab, in the XML group, click Schema.

    Note To display the Developer tab, do the following: Click the Microsoft Office Button button image, and then click Word Options. Click Popular, and then under Top options for working with Word, select the Show Developer tab in the Ribbon check box.

  2. Click Add Schema, browse to the XML Schema that you want to add to the Schema Library, and then click Open.
  3. In the Schema Settings dialog box, select the options that you want, and type a name for the Schema in the Alias box.

    Note The name that you type for the alias appears in the list of available Schemas in the Templates and Add-ins dialog box.

  4. Click OK.

Attach a Schema to a document

After there are Schemas in the Schema Library, you can attach a Schema to a document.

Note You can attach more than one Schema to a document. Elements from all of the attached Schemas are available in the list of elements in the XML Structure task pane.

  1. Open your document in Word.
  2. On the Developer tab, in the XML group, click Schema.
  3. In the Checked schemas are currently attached box, select the check box for each XML Schema that you want to attach to the document.

Apply XML tags to document content

When a custom XML Schema is attached to a document, the XML Structure task pane provides a list of elements that are defined in the Schema. You apply XML tags to the document by selecting document content and then choosing an element from the list. If the Schema defines attributes for an element, you can specify these as well in the XML Structure task pane.

Add or remove XML tags

Do one of the following:

Add XML tags

  1. In your document, highlight the content that you want to tag.

    You can select a word, phrase, paragraph, cell, row, column, field, picture, or object in the document.

  2. In the XML Structure task pane, in the Choose an element to apply to your current selection list, select an element.

    Note To limit your choices of elements and attributes, select the List only child elements of current element check box.

  3. If necessary, specify an attribute for the element.

    show How?

    1. In the XML Structure task pane, under Elements in the document, right-click the element, and then click Attributes.
    2. In the Available attributes list, select the attribute that you want.
    3. In the Value box, type the value of the attribute.
    4. Click Add.

Remove XML tags

  1. In the XML Structure task pane, select the Show XML tags in the document check box.
  2. In the document, rest the pointer on a Start of element name or End of element name tag.
  3. Right-click, and then click Remove element name tag to remove the tag without deleting its content.

    Note Each element has a start tag and an end tag. These tags can be removed as a pair only.

Show or hide XML tags

Print XML tags

  1. Click the Microsoft Office Button button image, and then click Word Options.
  2. Click Advanced.
  3. Under Print, select the Print XML tags check box.

Display XML element names

If your document contains empty XML elements, you can view these elements by name in the body of the document, even when the Show XML tags in the document option is turned off.

  1. On the Developer tab, in the XML group, click Schema.
  2. Click XML Options.
  3. In the XML view options section, select the Show placeholder text for all empty elements check box.

Tip You can specify your own placeholder text for elements by right-clicking a tag, clicking Attributes, and then typing text in the Placeholder text box.

Prevent accidental deletion of XML elements

If you want users to be able to edit the data in an XML document without editing or deleting the XML tags, you can use the document protection feature to make only the content of the XML elements available for editing, and not the tags themselves.

  1. In the XML Structure task pane, select the Show XML tags in the document check box.
  2. On the Developer tab, in the Protect group, click Protect Document.
  3. In the Protect Document task pane, select the Allow only this type of editing in the document check box, and then select No changes (Read only).
  4. In the document, select the contents of an element.
  5. In the Protect Document task pane, select the Everyone check box in the Exceptions (optional) section.
  6. Repeat steps 4 and 5 for each XML tag whose content you want users to edit.
  7. Click Yes, Start Enforcing Protection.
  8. Do one of the following:
    • To assign a password to help prevent the accidental removal of the document protection, type a password in the Enter new password (optional) box, and then confirm the password.
    • To encrypt the document so that only authenticated owners of the document can remove the protection, click User authentication.

Validate XML data

Word can validate an XML document according to the rules of an XML Schema that is attached to the document. Schema violations are shown in the XML Structure task pane, and they are also indicated in the document.

Tip To help avoid validation errors while you apply XML tags, select the List only child elements of current element check box in the XML Structure task pane.

  1. On the Developer tab, in the XML group, click Schema.
  2. Under Schema validation options, do one or both of the following:
    • To enforce validation, select the Validate document against attached schemas check box.
    • To prevent Word from saving a document that is not valid, clear the Allow saving as XML even if not valid check box.
  3. Use the XML Structure task pane to check the validity of a document while you're working on it. Icons next to each element indicate whether the element is invalid and the nature of the violation. Additionally, Schema violations are indicated in the document with purple wavy lines.

Turn off purple wavy lines

By default, Schema violations are marked in the body of the document with purple wavy lines. To turn this feature off, do the following:

  1. On the Developer tab, in the XML group, click Schema.
  2. Click XML Options.
  3. Under Schema validation options, select the Hide schema violations in this document check box.

Save Word formatting and properties along with custom XML data

If you want to preserve the page layout and formatting of the Word document, as well as custom XML data, use this procedure to save the document.

  1. Click the Microsoft Office Button button image, and then click Save As..
  2. In the Save as type box, click Word 2003 XML Document.
  3. In the File name box, type the document name.
  4. If you want to use an Extensible Stylesheet Language Transformation (XSLT) to format the document, select the Apply transform check box, and then click Transform to locate the XSLT file.

    Caution If you apply an XSLT when you save the file, Word discards any data that the XSLT does not use.

  5. Click Save.

Save custom XML data only

When you save only the custom XML data, anything in the document that is not tagged with an element from the attached Schema will be discarded in the resulting XML file.

  1. Click the Microsoft Office Button button image, and then click Save As..
  2. In the Save as type box, click Word 2003 XML Document.
  3. In the File name box, type the document name.
  4. Select the Save data only check box.
  5. If you want to use an Extensible Stylesheet Language Transformation (XSLT) to format the document, select the Apply transform check box, and then click Transform to locate the XSLT file.

    Caution If you apply an XSLT when you save the file, Word discards any data that the XSLT does not use.

  6. Click Save.