Inserting XML Markup [Word 2003 XML Reference] --  Microsoft Office Word 2003 XML Software Development Kit

Inserting XML Markup [Word 2003 XML Reference]

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.

If you have an XML schema attached to your document, you can apply Extensible Markup Language (XML) tags around pieces of the content of the document. You can also remove XML tags without deleting content in the document.

Adding XML tags to a document

You can add XML tags to your document either directly in the document or by using the XML Structure task pane. In each case there are a few things to note:

  • To apply XML element tags to text in a Microsoft© Office Word 2003 document, the document must be attached to an XML schema. For more informaiton, see Attaching an XML Schema.
  • XML markup in Word must be always be well-formed. For example, the end of an XML tag must not overlap with the start of another. It is assumed you are familiar with the concept of well-formed XML. A complete description of what makes XML well-formed is beyond the scope of this topic.
  • After you select the content you to which you want to apply an element, Office Word 2003 makes available only elements defined by the XML schema associated with that selected content. For example, if you select content contained in a <customer> tag, then only elements defined within the <customer> element in the associated schema are available to choose from.

Adding XML tags directly in the document

  1. In the XML Structure pane, select the Show XML tags in the document check box.

  2. In your document, highlight the content that you want to tag with XML markup. You can select a word, phrase, paragraph, cell, row, column, field, picture, or object in the document.

  3. Right-click the highlighted content, select Apply XML element, and then choose one of the elements.

    Note  To view all elements and attributes defined anywhere in the XML schema, uncheck the List only child elements of current element check box in the XML Structure task pane.

  4. If desired, specify attributes for the element. To do so, rest the mouse on its start or end tag, right-click, and then select Attributes. See Adding XML Attributes for more information.

Adding XML tags with the XML Structure task pane.

  1. In your document, highlight the content that you want to tag with XML markup. You can select a word, phrase, paragraph, cell, row, column, field, picture, or object in the document.

  2. In the XML Structure pane, in the Choose an element to apply to your current selection box, click an element.

    Note  

  3. If desired, specify attributes for the element. To do so, rest the mouse on its start or end tag, right-click, and then select Attributes. For more informaiton, see Adding XML Attributes.

Removing XML tags from a document

You can remove XML tags from your document either directly in the document or by using the XML Structure task pane.

Removing an XML tag from within document's contents.

  1. If XML tags are not displayed in your document, then select the Show XML tags in the document check box in the XML Structure pane.

  2. In the document, rest the mouse pointer on a Start of Tag Name or End of Tag Name tag.

  3. Right-click, and then click Remove Tag Name tag to remove the tag without deleting its content.

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

Removing an XML tag by using the XML Structure task pane.

  1. If the XML Structure task pane is not already displayed, choose Task Pane from the View menu, and then choose XML Structure from the menu at the top of the task pane.
  2. Right-click the tag you want to remove, and choose Remove [name] tag.

Inserting data from an existing XML file

If you're starting with a new document, you can open any valid XML file in Office Word 2003 and then begin editing it. You can insert data from an existing Extensible Markup Language (XML) file into a Office Word 2003 document that already exists by using one of the two following methods.

Insert the contents of an existing XML file directly into the document.

  1. Point to where you want to insert the file.
  2. On the Insert menu, click Insert File.
  3. In the Files of type box, click XML Files.
  4. Browse to the folder where the file is stored, and then click the file.
  5. Click Insert.

Create a refreshable link to the contents of an external XML file within the document.

  1. Place the insertion point where you want to insert the data.

  2. On the Insert menu, click Field, and in the Field names box, click IncludeText.

  3. In the Filename or URL box, type the name of the file, including its system path or URL.

  4. Check the Text converter check box and type a value of xml into its associated text box.

  5. Select the Namespace mappings check box, and type a namespace in the format xmlns:variable="namespace". For example, xmlns:a="resume-schema". Note that the prefixes used in the original XML file do not matter and cannot be relied on in the XPath setting of the Field dialog. All namespaces whose elements are referenced in the XPath expression text box must be re-mapped to new prefixes here. For example, if your file has a namespace declared xmlns:z=myNamespace, you must still map the namespace to a prefix (any prefix) again in order for XPath to use it. Here, "a", "hello", and "z" are all valid mappings to the "myNamespace" namespace.

    To use multiple namespaces from the same file, separate each by a space.

    Note  This option is only available in Microsoft Office Word 2003 Professional.

  6. If you want to insert only a fragment of data rather than the whole file, select the XPath expression check box, and then type the XPath expression in the box provided. For example, a:Resume/a:Name specifies the Name element in the root element Resume. Be aware that the XPath expression is not verified by Office Word 2003 and is assumed to be a valid XPath expression.

    Note  This option is only available in Office Word 2003 Professional.

  7. If you want to use an Extensible Stylesheet Language Transformation (XSLT) to format the data, select the XSL Transformation check box, and type the name of the file, including its system path or URL. The XSLT reference typed in here overrides any XSLT reference in the source XML file.

    Note  This option is only available in Office Word 2003 Professional.

  8. Click OK.

This option uses the IncludeText field. While it's mentioned here as a way to insert an XML file, it should be noted that you can use it to complete powerful tasks by displaying any or all of an external XML file with an applied XSLT transform within Office Word 2003. For example, this means you perform useful tasks such as creating Office Word 2003 documents that are summaries of InfoPath status reports or a Office Word 2003 document that displays a nicely formatted version of some arbitrary XML data from a backend system.

Understanding block-level tags and inline tags

In the main document window, a block-level tag is preceded and followed by a line break, and can contain block elements such as paragraphs and tables. An inline tag, on the other hand, flows inline with the text that precedes and follows it. There is nothing in an attached schema that makes an XML tag a block-level tag or an inline tag; this depends on the text selected when the user applies an XML tag from the XML Structure task pane to the selection. If the selection is a block element such as a paragraph or a table, then the tag becomes a block-level tag; otherwise, it becomes an inline tag. You can visually see the difference when XML tags are displayed: block tags appear with a clear background, and both their begin and end tags contain the tag's name. Inline tags appear with a light magenta background with their names only in the start tag. Inline tags' end tags do not have text in them.

It is important to note that an inline XML tag is inside a paragraph, whereas block-level tags contain paragraphs. For this reason, inline tags can only contain other XML items inside the tag, such as text, fields, and other forms of formatting. Therefore, if you try to insert a paragraph mark inside an inline XML element, Office Word 2003 correctly does not add the paragraph mark inside the XML element. Rather, Office Word 2003 moves the text that follows the paragraph mark outside the XML element. This is proper and expected behavior in order for Office Word 2003 to maintain well-formed XML. If you find yourself in a situation where you are trying to add a paragraph mark inside an inline XML element, you can instead use an XML block level tag. To do this, select the whole paragraph or the whole paragraphs that you want, including the paragraph marks for each paragraph. In the XML Structure pane, in the Choose an element to apply to your current selection list, click an XML tag. For more specific information on block-level and inline tags see Understanding Word's XML Markup.

More Information

For more information on the option settings used to configure the XML features in Word, see Understanding XML Options. For suggestions on troubleshooting problems that may occur while using these features, see Troubleshooting XML Documents in Word.

For additional information and examples that describe or demonstrate the end-to-end process of working with XML documents in Microsoft Office Word 2003, please see the following articles:

Editing XML Data with Microsoft Office Word 2003 and Microsoft Office Excel 2003

Creating and Applying an XML Resume Template in Microsoft Office Word 2003

Microsoft Office Word 2003 XML: Memo Styles Sample

XML and Microsoft Office Word 2003: Writing a Trip Report

©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