XSLT: XML Transformers!

Once you're in XML land, 80 percent of what your applications will do involves converting one type of XML to another. For instance, your domain-specific language (our movie review XML format) will have to be converted into many different XHTML (XML-compliant HTML) variants for different versions of Web sites (which you might need if you were supporting many types of browsers on many types of devices), including Wireless Markup Language (WML, another XML derivative) for delivery to WAP phones. In addition, the domain-specific language may be packaged for syndication with the Information Content Exchange (ICE) language (again, XML-derived) or converted into another data exchange language and so forth and so on. One immensely useful and exciting standard developed by the W3C is the Extensible Style Language or XSL and its derivative language, XSLT or XSL Transformation. XML processors based on the XSLT grammar can be used to convert (transform) between different XML-based languages quickly and easily.

Regular Expressions

At times you may be tempted to use regular expressions to transform bits of XML into other bits of XML. Regular expressions are essentially markup language for describing strings that enable you to search for and replace them in documents. For instance, the regular expression /foo/ searches for the string “foo” wherever it occurs in whatever is being searched.

It's especially tempting to make use of regular expressions in scripting languages such as Perl, which makes it almost impossible not to use them. Although the XML community generally turns up its nose at regular expressions, I have found it useful to make use of regular expressions for transformation of XML in limited cases.

Regular expressions have the advantage of being fast; you don't need a parser, a tree, an object model, or anything like that. When you use regular expressions to convert XML, you're not guaranteed to get valid XML on the output side, which can cause problems that you might not even discover until months after a system is operational.

A good rule of thumb for the use of regular expressions is to ask yourself, “Will anything ever parse or otherwise attempt to treat the output of what I'm converting as an XML document?” If so, I strongly recommend that you not use regular expressions and use the more heavyweight but reliable XSLT engines. Learn from my mistakes! The bother you think you're saving yourself now by using regular expressions will revisit you tenfold when you find that half the XML in your database is corrupt, unparsable garbage. Hey, don't say I didn't warn you.

If, however, you're converting bits of XML into bits of some other data-exchange language (such as EDI or a messaging system like MQ Series), regular expressions might be your ticket. XSLT can do this kind of conversion, but regular expressions may be faster and more efficient.


Transformation has always been a huge part of dealing with SGML and XML-based languages at the server side. This process usually consists of building a rule-based transformation script that acts within an XML parser to execute the transition. I remember writing this sort of script within a Perl-based parser back in 1995; there's nothing amazingly new about transformations of this kind. There's an important difference with XSL Transformations: XSLT can be described as XML Transformation via example. You feed a source XML file and an XSLT template into an XSLT processor, and the processor does the work of figuring out how to build the transform. (The XSLT template shows the XSLT processor how you want your result document to appear.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.118.95