Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide | |
Comparing XSLT to Cascading Style Sheets
Although XPath, CSS, and XSLT can work together to transform an XML data set, they each have a different role. The following is a brief review of these technologies and their roles.
- XPath formulates an expression to select a specified piece of data or data set. An XPath expression typically serves as a pattern to match this specific data in the source document.
- Cascading style sheets provide a centralized and modular way to format HTML output. Although CSS can be linked directly to an XML source document, it is more effective to use CSS as a compliment within XSLT.
- XSLT is the method by which you manipulate and transform (i.e., add, delete, query, sort, etc.) the XML data in a source document. XSLT uses XPath expressions to work with data elements. The output of XSLT transformations is typically another XML document, an HTML page, a text file, or a database entry.
This means that the real purpose of XSLT is not to format data, but to transform and manipulate data. The formatting aspect is best left for CSS, especially for large and complex documents.
If the content of an XML document can be displayed in the order as specified in the source document, you can use CSS without any help from XSLT. To display the output in any other order, you must use XSLT, possibly with the help of CSS.
The following sections summarize the advantages and disadvantages of XSLT and CSS.
Advantages of XSLT
- Can reorder, add, or delete elements. This is because XSLT is aware of the structure of the XML document.
- Can incorporate content from more than one document, or split a single document into many documents.
- Able to access and manipulate the comments, processing instructions, and attribute names and values in an XML document.
- Written in XML, so there is no third language to be learned.
- Offers programming-like logic, which includes an almost infinite variety of conditional statements. Can perform operations, such as arithmetic as well logical operations.
- Can include text and graphics.
- Can generate a variety of output: static or dynamic HTML, XML, RTF, PDF, comma-separated-values (CSV) flat files, or a stream of XSL Formatting Objects (XSL-FO).
Disadvantages of XSLT
- XSLT is not a style language in itself. Therefore, the application that handles the output from XSLT must be able to style it accordingly.
- Requires character escaping to output special markup characters, such as < and &.
- Uses more memory and processor power, because reordering, adding, deleting, and manipulating elements requires a tree representation of the document in memory.
- Is more difficult to learn to use.
Advantages of CSS
- Is easier to use than XSLT.
- Is suitable for simple documents.
- Uses less memory. Because it cannot reorder a document, CSS does not build a tree representation of the document.
- Begins displaying the document as soon as it begins processing the document.
- Has widespread support in the developer community.
Disadvantages of CSS
- Cannot reorder, add, delete, or perform operations on elements.
- Offers no access to non-elements, such as attributes and their values, processing instructions, and so on.
- Cannot style non-textual content (such as images) referred to in an XML document.
- Supports limited features of CSS2 for some applications.
- Uses a different syntax than XML, and therefore requires completely separate parsers and APIs.
- Outputs only data suitable for viewing in a Web browser.
Other Resources
Cascading Style Sheets