Memo Styles Sample [Word 2003 XML Reference] --  Microsoft Office Word 2003 XML Software Development Kit

Memo Styles Sample [Word 2003 XML Reference]

About this sample

Important  The information set out in this topic is presented exclusively for the benefit and use of individuals and organizations outside the United States and its territories or whose products were distributed by Microsoft before January 2010, when Microsoft removed an implementation of particular functionality related to custom XML from Word. This information may not be read or used by individuals or organizations in the United States or its territories whose products were licensed by Microsoft after January 10, 2010; those products will not behave the same as products licensed before that date or licenses for use outside the United States.

This sample is organized to explain all the different ways to work with XML in Microsoft© Office Word 2003. Each of the steps builds upon the previous step. Begin by opening the file Plain Memo.doc in Office Word 2003, and then progress in order through each of the steps to learn about using schemas, transforms, saving XML files, opening XML files, and many other XML features in Office Word 2003. If you installed the Word XML SDK onto your computer, then the Plain Memo.doc file is located in the program group in which the Word XML SDK is installed under Samples\Memo Styles Sample. If you are viewing this on the Web, then you can download the files from Word 2003: XML Memo Styles Sample.

Adding and attaching a schema

Attaching a schema to the current document adds it to the Schema Library and allows you to use it to mark up a Office Word 2003 document.

  1. On the Tools menu, click Templates and Add-Ins.

  2. Click the XML Schema tab.

  3. Click Add Schema to install the sample memo XML schema.

  4. Click Browse... and then navigate to the folder where the memo.xsd file is located ("local_drive:\Program Files\Microsoft Office Documentation\Microsoft Word XML Software Development Kit\Sample" or wherever you installed the SDK), click the file, and click Open.

  5. You are asked if you want to give the schema an alias. Type "Memo Example" and click OK.

    Note  When you install a schema in Office Word 2003, it is listed in the Schema Library and Templates and Addins dialogs by its namespace. If an alias is specified, however, Office Word 2003 refers to the schema by that instead. This is helpful since most namespaces are long and complex.

  6. Make sure the Memo Example schema you just added is selected under Schemas in the Schema Library dialog box and then click OK.

  7. When the new schema is installed, it is visible in the box under Available XML schemas. Ensure the check box is selected and click OK in the Templates and Add-Ins dialog to attach the schema to the current document.

  8. You should now be returned to the document, and the XML Structure task pane should be visible.

Adding XML to the document

Now you can mark up the document with XML.

  1. Bring up the XML Structure task pane (if it is not already up). You can do this by selecting Task Pane on the View menu. At the top of the task pane, where the title is, click the drop-down list, and then click XML Structure.

  2. Select the entire document (CTRL+A), and click the memo element located in the box at the bottom of the XML Structure task pane under Choose an element to apply to your current selection.

    Note  If the entire document is not selected, Office Word 2003 may ask you if you want to apply this element to the entire document. Click Apply to Entire Document.

  3. You now have XML in your document. The XML Structure task pane is useful in working with the XML in your document. Let's quickly go through the different portions of the task pane.

    • Elements in the document: This box at the top of the task pane is where you can see a tree view of the XML you apply to your document. If you click on an element in the tree, Office Word 2003 selects that element in the document. You can use this tree view to manipulate the element itself. If you right-click an element, you see:
      1. A description of any schema validation errors on that element (if they exist). In the case of our memo example, the memo element has an error because it is expecting more elements inside of it. We fix this error later in the task.
      2. Remove [tag name] tag. You can use this option to remove the tag from the document.
      3. Attributes. You can use this option to bring up a dialog allowing you to add or remove attributes to your element.
    • Show XML tags in the document. This check box is located directly below the Elements in the document tree view. It specifies whether to display XML tags directly in the document. By turning this option off the XML tags are not visible, though they are still present in the document.
    • Choose an element to apply to your current selection. This control lists the elements that you can apply to the text. The elements from which to choose are listed in the order they appear in the schema file. There are also two small buttons (an up arrow and a down arrow) that allow you to adjust the size of this list box.
    • List only child elements of current element. This option controls what is displayed in the Choose an element to apply to your current selection list box just described. If this is cleared, every element displays instead of just the children of the current selection.
    • XML Options. This opens the XML Options dialog box.
  4. Now that you are familiar with the XML Structure task pane, let's move on. Select the name in the document's To: field (John Doe) and click the to element located in the list box at the bottom of the XML Structure task pane. The to element is applied to the name.

  5. Select the name in the From: field (Jane Doe) and click the from element located in the box at the bottom of the XML Structure task pane.

  6. Select the subject of the memo (My memo to all) and click the subject element located in the box at the bottom of the XML Structure task pane.

  7. You can also apply elements by right-clicking. Select the priority (High) located in the priority line. Right-click the selection, point to Apply XML element, and then click the priority element.

  8. Select the date (1/15/2003). Right-click the selection, point to Apply XML element, and then click the date element.

  9. Select the body text of the memo. Right-click, point to Apply XML element, and then click the body element.

Saving the XML document

There are a number of options for saving this file. If you want to use this as a template, for example, you can save it as a standard Office Word 2003 template file (*.dot), and it retains the XML structure you applied. You can also save it as an Office Word 2003 document (*.doc) or an HTML (*.htm, *.mht) file. Each also preserves the XML structure.

A new option available in Office Word 2003 is to save this file as XML.

  1. To save the memo as an XML file, open the Save As dialog box.
  2. Change Save as type to XML Document.
  3. Select the Save Data Only check box. This indicates to Office Word 2003 to only save the XML applied and not the WordprocessingML describing the document's formatting information.
  4. Type a file name, and save the file.
  5. Office Word 2003 prompts you to confirm that you want to save the document as data only. Click Continue to save the new XML data only.
  6. This file you saved is an XML file containing only the XML elements you inserted into the file, and the text content of the file.
  7. Use Microsoft Internet Explorer to open the XML file you saved and look at the results.

This file contains no Word formatting information, so if you open it back up in Office Word 2003, all the layout and formatting is lost. (This is not the case if you have an XSL transformation though, which is explained later in this tutorial).

Another option is to save the XML file along with the Microsoft Word XML document markup. The XML from Word (e.g. WordprocessingML) is all in the Word namespace, and the XML you added is all in its own namespace. This makes it easy to work with the XML you marked up, while still preserving the layout and structure of the file.

  1. To save the XML memo with the Word XML document schema, open the Save As dialog box.
  2. Change Save as type to XML Document.
  3. Make sure the Save Data Only check box is cleared.
  4. Type a new file name, and save the file.
  5. Use the Internet Explorer or a text editor to open the XML file you saved and look at the results. You see all the Word information for this document, and if you scroll down into the w:body tag, you start to see XML from the memo schema.

Schema validation

This shows you a little more on how to work with the built-in schema validation in Word.

After completing the previous steps, your document should be valid according to the schema. You will know your document is valid because there are no validation icons beside any element in the XML Structure task pane. If you have a schema validation error, the error displays in the tree view of the XML Structure task pane.

Let's make this document invalid to see what an invalid document looks like. The memo schema has a few restrictions set on it. The priority element only allows three possible values: "Low," "Medium," and "High."

  1. Select the content of the priority element ("High") by clicking the priority element in the tree view of the XML Structure pane. When you click the priority element in the tree view, "High" should be selected in the document.
  2. Delete the current selection and type "Urgent" instead. "Urgent" should now be the contents of the priority tag.
  3. Notice in the tree view in the XML Structure pane that priority now has an icon shaped like a diamond with yellow corners to its left. If you hover over that icon, you see the warning: "'Urgent' violates enumeration constraint of 'Low Medium High'". This error comes directly from the schema file.
  4. Select the contents of the priority tag and change it to "Low" (this value is case sensitive). The error should now disappear.

Setting up a template

There are a couple of additional things you can do to make this template easier for people to use. This shows you how to only allow editing within certain XML tags, and also how to create placeholder text.

For most people using your template, XML is not familiar and the tags may be more of a hindrance than a help. That is why you probably want to have the tag view turned off when people are filling out this document. That can lead to problems though:

What if the contents of a tag are deleted? How will the user know where to type? You can solve this problem by using placeholder text. We describe that in this section.

If the tags are off, how can I ensure that people won't accidentally delete them? You can solve this with the new document protection feature described later in this topic.

  1. Turn on placeholder text. You can do this by clicking on the XML Options link at the bottom of the XML Structure task pane.

  2. In the XML Options dialog, find the option labeled Show placeholder text for all empty elements. Turn this option on by checking the box and clicking OK in the XML Options dialog.

  3. To see how placeholder text works, delete all the contents of the to element, but don't delete the to element itself. You can easily do this by just clicking the to element in the document. The contents are selected, but not the element itself. Now press DELETE.

  4. Now that the to element is empty, turning the tag view off means that the element is not visible. Because of what we did in step 3, however, this no longer happens. Turn the tag view off by either clearing the Show XML tags in the document option in the XML Structure task pane, or you can also just use the shortcut CTRL + SHIFT + X, which toggles the tag view on and off.

  5. Notice that when you turn the tag view off, there is still a visible field for the to element. You can click the placeholder text for the to element and start typing. The content you type is placed inside of the to element. If you now delete the content you typed, the placeholder text is visible again.

  6. You can also customize what is displayed in the placeholder text. To do this, right-click the to element in the tree view of the XML Structure task pane and choose Attributes.

  7. You should now be in the Attributes dialog box for the to element. At the bottom of the dialog box there is a text box where you can specify the placeholder text you want to use. Fill this in and click OK. The placeholder text in the document should now update.

    Note  Using the Show placeholder text for all empty elements option is not necessary to use placeholder text. Once you specify placeholder text for a particular element in that element's Attributes dialog box, that individual element always uses placeholder text, regardless of the Show placeholder text for all empty elements option.

    Note  Also be aware that specifying placeholder text in an element's Attributes dialog affects only that individual element and does not work for other elements of that same name throughout the document.

Now that you added placeholder text to the elements in your document, let's lock it down so that only specific areas are editable.

  1. On the Tools menu, and click Protect Document.
  2. This is the document protection task pane. Here you can lock down the formatting and the editing of a document.
  3. Formatting restrictions: This option allows you to lock the document down so that no direct formatting is allowed and that only styles you specify can be used to format a document. We will not use this feature in this tutorial.
  4. Editing restrictions: This option allows you to specify the level of editing you want to allow on your document. For this tutorial, we make the entire document read-only, and then give write permission only to certain portions we want people to edit.
  5. Check the check box labeled Allow only this type of editing in the document: and directly below that select No changes (Read only) from the drop-down menu.
  6. Select the contents of the to element and select the Everyone check box in the Protect Document task pane. You now gave permission for Everyone to edit the contents of the to element.
  7. Now, let's start enforcing protection. Click Yes, Start Enforcing Protection at the bottom of the Protect Document task pane.
  8. You are asked if you want to enter a password. This is optional. For now, just press OK.
  9. You should now see that the contents of the to element are shaded yellow. This is the only area that you can currently edit. You can not delete any of the XML tags or edit any of the other content. This is an effective way to make sure that people don't accidentally delete the structure you applied when they are filling out your template.

Opening the XML Document

  1. Open Office Word 2003.
  2. Click Open and browse to the location of the data-only XML file you saved in the Saving the XML Document section.
  3. Open the XML file in Word.
  4. You will notice that the formatting that was originally applied to the file is now gone. The file is opened in the default view Data Only. This view is used by Office Word 2003 when it opens any XML file for which it does not have an XSL transformation.
  5. Let's choose a different view. You can click on the Browse option and select a transformation. In the XML data views task pane, click Browse and navigate to the location where you saved the sample files (For example, "local_drive:\Program Files\Microsoft Office Documentation\Microsoft Word XML Content Development Kit\Sample"). Select any of the three transforms (elegant.xsl, professional.xsl, or contemporary.xsl).
  6. You can see that Office Word 2003 now opens the XML file through the transformation and the file displays in a more formatted way.

Now, let's use the Schema Library to indicate to Office Word 2003 to always use that transform when it opens files of this type (Memo).

Adding XSL transform to the Schema Library

Office Word 2003 allows you to associate different resources with namespaces. Namespaces are the way XML files can identify what type they are. Office Word 2003 refers to the root namespace of any XML file it opens, then refers to the schema library to see what XSL transformations are available and the location of the XML schema file so that it can validate the document.

  1. On the XML Schema tab, click Schema Library.

  2. Click the memo schema (urn:schemas-microsoft-com.office.demos.memo). Note that if you gave the schema an alias when you first added it, it is listed by its alias rather than its namespace.

  3. Make sure that Word is selected in the Use solution with box. This specifies that the solution you are about to use is for Office Word 2003.

  4. Click Add Solution to add a solution. In this case, the transformations for the memo sample.

  5. Browse to the location where you saved the sample files and select any of the three XSL files (elegant.xsl, professional.xsl, or contemporary.xsl).

  6. In the Edit Solution Properties dialog box, in the Alias box, give the transform an appropriate name.

  7. Click OK. The solution is added to the Select a solution list box on the XML Schema tab.

  8. Repeat these steps for the other two transforms located in the same folder.

  9. You can also specify which XSL Transformation you want to use by default by selecting the transformation you want from the Default solution list.

  10. You have not made it so that anytime an XML file in the memo namespace is opened in Office Word 2003, there will be three transformations available to use. Let's try opening the document again.

  11. Click Open in Office Word 2003 and browse to the location of the data-only XML file you saved.

  12. Open the XML file in Office Word 2003.

  13. The XML Document task pane displays the different transformations available.

  14. Click the different transforms and see how Office Word 2003 reformats the document automatically.

    Note  The process of setting up XSL transformations described above is meant for people working directly with the XSL files and who are familiar with XML. There is a very easy way to configure a user's Schema Library automatically without requiring them to use the Schema Library dialog. For more information about this, see manifests in the Smart Document Software Development Kit.

©2004 Microsoft Corporation. All rights reserved. Permission to copy, display and distribute this document is available at: http://msdn.microsoft.com/library/en-us/odcXMLRef/html/odcXMLRefLegalNotice.asp