David Silverlight

Subscribe to David Silverlight: eMailAlertsEmail Alerts
Get David Silverlight: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: XML Magazine

XML: Article

Shedding A Little Light On XML

Shedding A Little Light On XML

This month, we'll focus on an important aspect of problem solving: finding the correct resources. The answers in this month's Q&A will demonstrate techniques from a variety of invaluable resources that have had a large influence, a few of which I'll mention here. In future columns others will come into play.

I'm a strong believer in networking among experts in the field. Oddly enough, most of my networking is done with people I've never met face to face. These days it just isn't necessary. In fact, a face-to-face meeting with a fellow computer geek at 3:00 a.m. isn't something I would desire, either.

There are a variety of folks I've had the opportunity and privilege to learn from, most of whom I've interacted with only through e-mail and/or messenger. Regardless of whether or not I'd recognize them as they walked down the street, I've benefited from their expertise on many an occasion. In this field it isn't always a matter of being able to solve a problem. Sometimes it's just knowing where to look when you need help breaking through a tough section of code.

Q:  How do I return the top highest x elements grouped from an unsorted nodeset?
A:  Simple? On the surface, perhaps. The implementation of this solution involves a binary search, recursion, and a modular implementation of templates. (The last point isn't absolutely necessary, but should become common practice to allow code                                                                 . reuse.)

This question generated a pretty long thread in the XSLTalk (an advanced XSLT/XPath programming group at http://groups.yahoo.com). Dimitre Novatchev, from VBXML.com, came up with an innovative solution. Due to its clever implementation, we'll refer to it as the "Novatchev Method." The algorithm is described below. Be sure to download the source code from the XML-J Web site; it's well worth checking out.

Why Is This Difficult?
The reason this is tricky is that, although it's easy to return the top x elements alone and to group elements together, it's not easy to do the combination without looping through all of the elements of each group. If the nodeset was very large, it would be a lot of unnecessary work.

Breakdown - 50,000 foot level

  • Create a delimited string from the nodeset from which you wish to extract the top x elements.
  • Parse through the dates using a single pass, finding the correct order for each date as you go. The key to performing this step efficiently is to use a binary search to quickly locate the sorted position in the string.
    -The binary search is implemented as a template, which can be included in any XSLT application that would require this functionality.
    -The end result is a sorted, delimited string, which is fed into a template to display as output.
PseudoCode Level:
  • Including templates
    The first section of code is the include section. Here we're incorporating templates that will be used in our solution. This is a technique that you should adopt if you haven't already done so. It allows you to use functionality such as searching and displaying (in this example) into this stylesheet and into any other stylesheet that fits the same requirement.
  • Recursive inline sorting
    The bulk of our work is performed using a template that will recursively make a single pass through our delimited date string and feed each date into our binary search template (see Listing 1).

    As each date position is located using our binary search, it's merged into its proper location based on the MergePosition and the length of the sort element (see Listing 2).

  • Display our sorted output
    Upon completion of our sorting, we send the delimited string to a template that's used for display. This template, which can be used to display the column of data, will take our delimited string and create a basic html table from it (see Listing 3).

    Although you've seen some segments of the source code in this answer, the full source code to this solution can be downloaded from the XML-J Web site.

Q:  Combining stylesheets: Should I use <xsl:import> or <xsl:include>?
A:  As was touched upon in Question 1, you can modularize your templates and reuse them across stylesheets. Taking this approach will aide you in code reuse and help to create a more generic, "black-box" view of the way you work with XSLT templates. That being said, the two mechanisms defined in the XSLT 1.0 spec for combining stylesheets are the top-level <xsl:import> and <xsl:include> elements. The differences between the behavior of these elements deal mainly with import precedence. Understanding them will help you decide which element to use in your stylesheet scenario.

The primary difference between the two is that when you use <xsl:import>, the import precedence of the stylesheet that's doing the importing has a higher precedence than the stylesheet that's being imported, whereas when you use the <xsl:include> the precedence is the same.

An error condition will be generated if you use <xsl:include> to incorporate a stylesheet that already exists in the importing stylesheet. The way the error is handled depends on the XSLT processor. Most processors will quietly handle the error by using the last template they encounter. Essentially, the last one found wins. In fact, the XSLT 1.0 spec states that "An XSLT processor may signal the error; if it does not signal the error, it must recover by choosing, from among the matching template rules that are left, the one that occurs last in the stylesheet."

In a nutshell, you should use <xsl:import> when your intention is to control precedence over templates and variables. You should use <xsl:include> in cases where precedence is not an issue.

Q:  How do I combine multiple XML documents?
A:  This question is one of the most common that come my way. Since many applications require data from more than one source, there is often a need to combine xml documents into a single data source before transformation. The more I play around, the more ways I find to accomplish this. I've decided to answer this by displaying a number of ways I've found to combine XML, along with the contributor who originally inspired the example. The case scenario for this answer is a set of XML documents that contain product data from different regions (NW, SW, etc.)

Elements of Design -
Displaying XML Data Graphically

We'll kick off the first issue of "Elements of Design" with a question: What's the most innovative thing you can do with XML? Each month we'll focus on a different XML implementation that shows off some of the novel applications of XML.

This month we'll be highlighting a method of visually displaying our XML data. In Figure 1, we show a graphic illustration of XML data containing sales numbers for various regions of a company. How is this done? Well, SVG is the underlying technology that's used to render this image. SVG, like many XML-based technologies, is still seeing only the beginnings of what it's capable of.

The full source code to the XML and XSL documents used for this demonstration and other SVG implementations can be found at Kurt Cagle's site at www.KurtCagle.com. Additional links to tons of SVG resources can be found at the SVG section of my own site, www.xmlpitstop.com.

You'll be able to find answers to questions such as:

  • What is SVG?
  • How can I display SVG in my browser?
  • What's the current status of the W3C SVG spec?
  • Others that are too numerous to answer in this column...
If you have an innovative, clever, or just downright cool example of any XML-centric code that you'd like to share with the rest of the world, e-mail me.

More Stories By David Silverlight

David Silverlight is the chief XML evangelist for Infoteria. He has
been working in the trenches for a number of years as a software
architect and consultant, specializing in database-driven Web
applications. He also maintains www.xmlpitstop.com, a resource for
XML examples, resources, and everything else XML.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.