Create dynamic images with SVG and XSL
Takeaway: Delivering GIFs and JPEGs via XML is no walk in the park, especially when compared to the ease of delivering Scalable Vector Graphics (SVG). Find out how to convert SVG images into an XSL template.
Editor’s note: This article originally appeared on Builder.com.
Delivering graphics with XML can be cumbersome if you're using older graphics formats, such as GIF and JPEG. But with Scalable Vector Graphics (SVG), it's a breeze. Because SVG is an XML grammar specification, you can create custom dynamic SVG images using just a little XSL.
The SVG template
The technique is based on creating an SVG template and modifying its contents to produce a new SVG image. To start the process, you’ll need a base image to work from. You can use a variety of tools, such as WebDraw from JASC, to create your starting image. In fact, you can also use any text editor because all SVG images are just XML.
Listing A shows a simple SVG button, which you’ll use as the base template for this process. If you load this document into an SVG browser, you’ll see a simple blue button that says Button. If you click the button, it will take you to the TechRepublic.com home page.
Source data as XML
You’ll begin by converting this SVG document into an XSL template. The template basically splits the image into two parts: The static part is the XSL template, and the dynamic part is an XML document you'll use to drive the transformation. There are two components to this image that you want to affect dynamically—the URL and the text of the button. A new XML document, shown in Listing B, contains the dynamic source data.
As you can see, the new button will sport the text Google and a link to the popular search engine. The next step is to convert the SVG image into an XSL template. Essentially, you’ll make the link and the button text dynamic by pulling that data from the XML document. Listing C shows the XSL template.
The XSL template has revamped the original SVG document and made it more dynamic. This is a simple transformation that uses only a single-element template. When processed, the link will be populated with the element /button/url in the source document, and the button text will be the value from the /button/text element.
Transforming XML into SVG
There are many ways to actually process the source XML and XSL template. In practice, you'll most likely use the Xalan APIs to transform the XSL and XML into an SVG document via programming. For demonstration purposes, use the Xalan command line. To run this process, type the following at a command prompt:
java org.apache.xalan.xslt.Process -IN custombutton.xml -XSL button.xsl -OUT custombutton.svg
The result of this process should look similar to the SVG document in Listing D.
Displaying SVGs on the Web
Contrary to popular belief, you can use SVG graphics on Web pages. Instead of being implemented as source files for <img> tags, they're implemented as embedded objects. Listing E shows a simple Web page that displays the new SVG image.
Print/View all Posts Comments on this article
|
|
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- VMware Infrastructure: A Guide to Bottom-Line Benefits VMware Frustrated by the high cost of maintaining or building ever-larger data centers? Get the facts you need to formulate your Virtualization Action Plan. Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
Designing the next killer product
Developing new ways to collaborate
Overseeing IT operations across a global organization
The biggest security threats facing companies in 2009
