Section 14.2.  Smart tags

Prev don't be afraid of buying books Next

14.2. Smart tags

Smart tags allow actions to be associated with words and phrases in a document. The "tags" don't have to be delimited, as XML tags are. Instead, they are defined by a program or by a lookup table that contains character strings and their associated actions. The product recognizes the matching strings in the document.

Instead of a specific character string, recognition could be based on a kind of pattern for strings that computer scientists call a regular expression. In this case, any character string in the document that matched the pattern would be recognized and considered a smart tag.

Alternatively, program code could be used to create more sophisticated recognition tests.

In all cases, though, when a user inserts or mouses over recognized text a small button appears. When clicked, a context menu pops up that contains actions relating to the recognized text.

Figure 14-5 is an example of the stock symbol smart tag that is built into Excel. The string "MSFT" in cell A1 has been recognized as a stock ticker symbol, so the smart tag i button is displayed next to the cell. Clicking the button caused the context menu to appear. The menu allows you to insert the stock price in the worksheet or to go to the MSN website for more information about the stock or the company.

Figure 14-5. A smart tag menu




Another built-in smart tag for Word recognizes full names of your Microsoft Outlook contacts. The context menu lets you send that person an e-mail or schedule a meeting with her.

14.2.1 Using smart tag lists (MOSTL)

The simplest way to implement smart tags is by creating an XML document that complies with the Microsoft Office Smart Tag Lists (MOSTL) schema. This document lists the text strings or patterns to look for, and the actions to show in the context menu when a smart tag is recognized.

An example of a MOSTL document for recognizing ZIP codes is shown in Example 14-11. While some MOSTL documents list literal text strings to recognize, this document specifies a pattern. The regular expression (\d{5}), which represents five digits, is the content of the exp element. This means that any five consecutive digits will be recognized as a ZIP Codes smart tag.

Example 14-11. A MOSTL document to implement smart tags
 <FL:smarttaglist     xmlns:FL="urn:schemas-microsoft-com:smarttags:list">   <FL:name>ZIP Codes</FL:name>   <FL:lcid>1033</FL:lcid>   <FL:description>Recognizes ZIP codes</FL:description>   <FL:moreinfourl>http://xmlinoffice.com</FL:moreinfourl>   <FL:smarttag type="urn:schemas-microsoft-com:smarttags#msproducts">     <FL:caption>ZIP Code</FL:caption>     <FL:re>        <FL:exp>(\d{5})</FL:exp>     </FL:re>     <FL:actions>        <FL:action >           <FL:caption>Look up shipping rates</FL:caption>           <FL:url>http://xmlinoffice.com/ship/{TEXT}</FL:url>        </FL:action>        <FL:action >           <FL:caption>Determine time zone</FL:caption>           <FL:url>http://xmlinoffice.com/tz/{TEXT}</FL:url>        </FL:action>     </FL:actions>   </FL:smarttag> </FL:smarttaglist> 

Each menu item is specified with an action element that contains a caption (the text that will appear on the menu) and a url that is referenced when you click the menu item. Our example might allow users to look up shipping rates to that ZIP code, or to determine the time zone of the ZIP code. The expression {TEXT} is used within the URL to mean that the recognized string (in this case the 5-digit number) will be included in the URL at that point.

Implementing this smart tag list is a simple matter of placing the XML document in your local smart tag lists directory, and restarting all smart-tag-compliant applications. The directory path is normally: Program Files\Common Files\Microsoft Shared\Smart Tag\Lists\

14.2.2 Using program code

Smart tags can also be implemented using program code, written with either Visual Studio.NET or Visual Studio 6.0.

While MOSTL documents can only specify actions as URLs, program code allows you to perform almost any action you can imagine. You can access Web services and databases, send emails, and insert or edit text or XML elements, among other things.

More sophisticated recognizers can also be written using program code, for example to only recognize valid ZIP codes, or only ZIP codes that appear in certain areas of a document.

Unlike smart documents, smart tags have no direct relationship with the Office features that allow the editing and viewing of XML documents. Smart tags can therefore be used in any Word and Excel documents, regardless of whether the documents are associated with a schema. Smart tags can also be used in PowerPoint, Access, Outlook, and Internet Explorer.

However, in Word and Excel documents that are associated with a schema, it can be useful to create smart tag solutions that are sensitive to the XML context. For example, you could write a smart tag recognizer that only recognizes ZIP codes when they appear inside a ZIP element, or client codes when they appear inside a billTo element.

A smart tag recognizer could be written to respond when a user types the word "problem" into the body element of a letter. The context menu could allow a choice among different canned "problem letter" phrases, which would be inserted automatically into the document.

Tip

Additional information about smart tags is included in the Smart Tags SDK, which can be downloaded from the Microsoft website.




Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net