Adding XML Attributes [Word 2003 XML Reference] -- Microsoft Office Word 2003 XML Software Development Kit
An XML attribute is defined as follows: A name-value pair, separated by an equals sign, included inside a tagged element that provides additional information about certain features of the element. For example, in the XML fragment shown below, the book element has a genre attribute that modifies or describes the book:
<book genre="autobiography" ... > <title>The Autobiography of Benjamin Franklin</title> ... </book>
The XML Structure task pane in Microsoft© Office Word 2003, and the main document window itself, does not display XML attributes or their values. For example, the schema
books.xsd may define a genre attribute for books:
<xsd:complexType name="bookType"> ... <xsd:attribute name="genre" type="xsd:string"/> ... </xsd:complexType>
However, the XML Structure task pane in Word displays only XML elements, as shown below. You can view an element's attribute values by moving the mouse over the element name.
Figure 1. The XML Structure task pane with attribute values displayed as the user hovers over an XML element.
Likewise, the main document window displays only XML elements and their contents, but not their attributes, as shown below.
Figure 2. The main Word document window displays only XML elements and their contents.
You can view an XML element's available attributes, and assign values to them, by invoking the Attributes dialog box. Right-click an XML element in the XML Structure task pane, or an XML element tag in the main Office Word 2003 document window (with XML tags displayed), and select Attributes to invoke the Attributes dialog box, shown below.
Figure 3. The Attributes dialog box displaying an XML element's attributes and their values.
The Attributes dialog box displays the following information:
- The URI value contains the namespace of the attribute currently selected in either the Available Attributes or the Assigned Attributes list. It is blank in the example shown because the selected genre attribute is defined in the empty namespace for the
- The Available attributes list contains attributes defined for the current element which are not defined in the current document. Since all available attributes of the book element in the example shown have defined values in the document, they appear in the Assigned attributes list below, and the Available attributes list is empty.
- The Type value contains the type of the attributes currently selected in either the Available Attributes or the Assigned Attributes list as defined by the associated XML schema,
books.xsd. In this example, the type of the selected genre attribute appears as "Custom type" because the genre attribute is not an XML simple type.
- The Value text box displays, and allows the user to enter or modify, the current value of the currently selected attribute.
- The Assigned attributes list contains attributes defined for the current element which already exist in the current document. Since all available attributes of the book element in the example shown are present in the document, they appear in the Assigned attributes list, and the Available attributes list above is empty.
- In the Placeholder text text box you can add custom instructional text for the currently selected element. This text appears whenever the contents of the element are empty in the current document and tags are turned off. In this example, the book element has sample placeholder text defined. If no placeholder text is added for an element here, then no placeholder text is displayed for that element. The following figure demonstrates how the placeholder text appears in the document.
Figure 4. Placeholder text defined in the Attributes dialog box for an empty element displays when Show XML tags in the document in the XML Structure task pane is unchecked.
Note To display placeholder text for all empty elements in the document automatically, click XML Options in the XML Document task pane and check Show placeholder text for all empty elements. This inserts default placeholder text consisting of the element's name enclosed by square brackets. For example, the default placeholder text for the book element is [book].
Figure 5. Default text for the empty <book> element displays when Show XML tags in the document in the XML Structure task pane is cleared when it has no placeholder text defined in the Attributes dialog box.
For more information about 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, see the following articles:
©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