This article assumes you're familiar with HTML, CSS, JScript, XML
Download the code (7KB)
VML Provides XML-based Graphics for the Web
Scott Howlett and Jeff Dunmall |
Vector Markup Language, new to Internet Explorer 5.0, lets you generate vector-based images on the fly using some simple XML. |
Most Internet developers generally focus on technology advancements that are happening on the serverthings like COM+
and queued components. But when we took our heads out of the sand, we realized that Microsoft® Internet Explorer 5.0 would be as important as Windows® 2000 in terms of its impact on Internet application development. This is primarily because Internet Explorer 5.0 shipped with improved support for Extensible Markup Language (XML) and two new XML-based offerings: Extensible Stylesheet Language (XSL) and Vector Markup Language (VML). In this article we'll introduce an XSL design pattern that is suited to developing Internet applications. Along the way, we'll show you the basics of VML. Finally, we'll introduce a sample application that produces a graphic representation of an organization chart on the fly. We'll not only make extensive use of VML, but also XML and XSL. This results in source data that is completely separate from any presentation code or processing. In a future article we will extend the organizational chart sample developed here to cover XSL pattern matching and support in Internet Explorer 5.0 for drag and drop operations. We'll also design a template for creating XSL documents that allows you to edit XML documents inside the browser. When we finish you'll have seen an end-to-end solution for managing organizational charts.
XML: Believe the Hype
|
![]() |
Figure 1: XML/XSL Design Pattern |
Using XSL, the results of changes can be shown to the user immediately without a round-trip to the server. The client can manipulate the data and display it as the user sees fit, while keeping the server out of the picture. The client now has the tools to quickly and efficiently manipulate an XML string. In the end, we have an alternative to data storage in a relational database. The new approach is shown in Figure 1.
Usage Case
Vector Markup Language
|
|
The full source is shown in Figure 3. VML is fully supported by Office 2000. This means that you can use Microsoft Excel, Word, and PowerPoint® to create pictures, then save the document in VML by default, along with a GIF or JPEG image. To make VML the only saved format, you have to make a simple change in your options. In Word, for example, go to Tools | Options, select the General tab, and click the Web Options button. The Pictures tab lets you check the "Rely on VML for displaying graphics in browsers" option.
A great application of VML is the creation of menus and navigational elements. You could use VML to create a graphic that would serve as the background for the image. Using XSL, you could then create the menu, dynamically inserting whatever menu text you want.
Nuts and Bolts
|
|
This code tells Internet Explorer that all tags beginning with v: are part of the VML namespace, as opposed to the default HTML namespace. The second item you should examine is the declaration of the VML behavior: |
|
This instructs the browser to pass all of the tags beginning with v: to the rendering object. If you omit either of these aspects of the documentdeclaring the namespace or declaring the behaviorthe VML will not be displayed. If you are having trouble producing VML output, start your debugging by looking for these elements.
VML Semantics
|
|
The coordsize attribute lets you redefine the coordinate space into appropriate units. In this case, it specifies that the square is spanned by four units (0 through 3) on the x and y axes. But this only defines the containing box of the shape. To define the shape itself, use the <v:path> element. The path element has an attribute, v, that defines the points to draw. You set v to a string that represents a list of vector-based drawing operations. The points are x,y values in the units of the coordsize attribute of the parent shape element. As part of the v attribute, you can use the letters m for moveto and l for lineto. The x attribute will close the path by joining the current point to the first point in the path, while e simply ends the current path. The <v:shapetype> element is used to predefine a shape that will be used elsewhere in the document. You must specify an ID attribute because this is how the <v:shapetype> is referenced elsewhere in the document. Usually a <v:shapetype> is used when more than one of the same shape is to be used in the document. This element does not produce any visible output. Instead, the syntax <v:shape type="#id"> is used. The following code defines an octagon that can be used repeatedly throughout the document. |
|
![]() |
Figure 6: Shapes |
The <v:textbox> element lets you display text inside a VML object. It allows rich HTML to be inserted, building on the existing features of HTML and CSS. This VML code draws the graphic shown in Figure 6, making use of the octagon shape type: |
|
In addition to these elements, there are also <v:line>, <v:polyline>, <v:roundrect>, <v:oval>, <v:curve>, and <v:arc> elements that make drawing simple shapes easier than using the <v:shape> and <v:path> combination. The details of the additional VML elements, of which there are many, are beyond the scope of this article.
Sample Application
|
|
The next step is to take the XML and transform it into HTML. There are many ways to do this, but the best way is to use XSL. XSL provides a simple yet powerful model for the transformation of one XML document to another. In this case, we're going to transform a client XML document into an XML-compliant HTML document. This is one of the tricks of XSL: the resulting document must be valid XML. Whereas you're probably used to using <BR> to start a new line, with XSL you must use <BR/> to keep the document valid. Dino Esposito's Cutting Edge columns in the June, July, and August 1999 issues of MIND describe the basics of XSL. To start, we can use XSL to generate HTML code that provides a textual representation of the organization. This is a good starting point as the XSL will be straightforward and the resulting application will be simple and effective. This is the typical approach you may have read about in other articles. The XSL could be applied on the server to handle noncompliant browsers, or sent down to the client to reduce server-side load. Browser-specific XSL could be applied, depending on the target, as Dino demonstrated in the Cutting Edge columns just mentioned. The resulting page would look something like Figure 7. |
![]() |
Figure 7: A Text-based Representation |
The most common approach to rendering the text-based representation in Figure 7 would involve pointing the browser directly to a source XML document that references a style sheet. To do this, you would place the following line at the top of your XML document: |
|
By doing this, Internet Explorer will load both the source XML and XSL documents and do the transformation for you. The document resulting from the transformation has to be a valid HTML document in addition to a valid XML document. This means that your XSL document has to contain all the appropriate tags to create a valid HTML document (<HTML>, <HEAD>, <BODY>, and so on). This approach, while good for debugging purposes, is limited for a number of reasons. It clutters your XSL, makes it impossible to use multiple style sheets in the same HTML document, and assumes the source is the XML data itself. While this is a common approach, there is a more flexible solution.
A Container Document
This solution also has great caching benefits. The XSL files get downloaded once and stay on the client. Each time the page is hit, the skeleton container page comes down with the dynamic data. To do the transformation programmatically on the client, there are three steps:
The first transformation uses header.xsl to create the page header. It displays the company name and places a horizontal rule below it. The second transformation displays the company structure, using indentation to depict the hierarchy. This approach is more flexible because other transformations can be used to display the company structure, like using VML to represent the organization. More generally, you can change the look and feel of one portion of your site by changing a specific XSL document.
XSL for VML
|
|
Inside the people template, we create the HTML table and a row in the table, and then apply the person template for each <PERSON> in the <PEOPLE> collection: |
|
In the person template, we create a new cell and then a round rectangle with the name and title of the person, using the <v:roundrect>, <v:fill>, and <v:textbox> VML elements. Next, we apply the people template shown in Figure 10, which introduces the recursive nature of this example. The result is shown in Figure 11. |
![]() |
Figure 11: An Organizational Chart Using XSL |
But we weren't happy with this. For large organizational charts, the picture was too big to fit on the screen, and we wanted to be able to focus in on a particular group by collapsing the branches we weren't interested in. To accomplish this, we first had to identify which persons had subordinates. Next, we wanted to provide a visual cue to the user about which nodes could and couldn't be clicked. Finally, we wanted to change the mouse cursor to a hand on a mouseover for persons that had subordinates. Once we had that figured out, all that was left was to write some code to show and hide different branches. Determining which person nodes had children was made easy by using a conditional test statement in XSL. The XSL code to adjust the mouse cursor looks like this: |
|
We did the conditional test using "PEOPLE/PERSON" because this would only apply the conditional formatting if there was a child <PEOPLE> collection that contained at least one <PERSON>. Similar XSL code is used to place a shadow on the persons with subordinates, giving the user good visual cues. The script to show and hide the different branches is relatively straightforward (see Figure 12). The end result displaying both XSL transformations is shown in Figure 13. Note that Bill Onliane has been clicked to roll up his subordinates. |
![]() |
Figure 13: Full-featured Organizational Chart |
There's some complex pattern matching in this example which we don't discuss in detail here. A future article will discuss XSL pattern matching in more detail.
Going it Alone
Conclusion
|
![]() |
Vector Markup Language (VML) Overview at: http://msdn.microsoft.com/standards/vml/default.asp |
From the January 2000 issue of Microsoft Internet Developer.