Create an XML document based on a custom Schema
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
Apply XML tags to document content
Save Word formatting and properties along with custom XML data
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:
- On the
Developer
tab, in theXML
group, clickSchema
.Note To display the
Developer
tab, do the following: Click theMicrosoft Office Button
, and then click
Word Options
. ClickPopular
, and then underTop options for working with Word
, select theShow Developer tab in the Ribbon
check box. - Click
Add Schema
, browse to the XML Schema that you want to add to the Schema Library, and then clickOpen
. - In the
Schema Settings
dialog box, select the options that you want, and type a name for the Schema in theAlias
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. - 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.
- Open your document in Word.
- On the
Developer
tab, in theXML
group, clickSchema
. - 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
- 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.
-
In the
XML Structure
task pane, in theChoose 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. - If necessary, specify an attribute for the element.
How?
- In the
XML Structure
task pane, underElements in the document
, right-click the element, and then clickAttributes
. - In the
Available attributes
list, select the attribute that you want. - In the
Value
box, type the value of the attribute. - Click
Add
.
- In the
Remove XML tags
- In the
XML Structure
task pane, select theShow XML tags in the document
check box. - In the document, rest the pointer on a Start of element name or End of element name tag.
- 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
- Press CTRL+SHIFT+X.
Print XML tags
- Click the
Microsoft Office Button
, and then click
Word Options
. - Click
Advanced
. - Under
Print
, select thePrint 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.
- On the
Developer
tab, in theXML
group, clickSchema
. - Click
XML Options
. - In the
XML view options
section, select theShow 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.
- In the
XML Structure
task pane, select theShow XML tags in the document
check box. - On the
Developer
tab, in theProtect
group, clickProtect Document
. - In the
Protect Document
task pane, select theAllow only this type of editing in the document
check box, and then selectNo changes (Read only)
. - In the document, select the contents of an element.
- In the
Protect Document
task pane, select theEveryone
check box in theExceptions (optional)
section. - Repeat steps 4 and 5 for each XML tag whose content you want users to edit.
- Click
Yes, Start Enforcing Protection
. - 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
.
- To assign a password to help prevent the accidental removal of the document protection, type a password in the
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.
- On the
Developer
tab, in theXML
group, clickSchema
. - 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.
- To enforce validation, select the
- 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:
- On the
Developer
tab, in theXML
group, clickSchema
. - Click
XML Options
. - Under
Schema validation options
, select theHide 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.
- Click the
Microsoft Office Button
, and then click
Save As
.. - In the
Save as type
box, clickWord 2003 XML Document
. - In the
File name
box, type the document name. - If you want to use an Extensible Stylesheet Language Transformation (XSLT) to format the document, select the
Apply transform
check box, and then clickTransform
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.
- 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.
- Click the
Microsoft Office Button
, and then click
Save As
.. - In the
Save as type
box, clickWord 2003 XML Document
. - In the
File name
box, type the document name. - Select the
Save data only
check box. - If you want to use an Extensible Stylesheet Language Transformation (XSLT) to format the document, select the
Apply transform
check box, and then clickTransform
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.
- Click
Save
.