XML: XSL style sheet for data: Push or pull? English Original content: Sample table drawing style sheet therefore, pull always better than pushing, right? Conclusion Reference Information About the author Related content: Subscribe to the developerWorks newsletter In the XML area also: Tutorial tools and product code and component articles View two XSL style sheets production technology and how to integrated it for data
Kevin Williams (Kevin@plueoxide.com) CEO, Blue Oxide Technologies, LLC 2002 May
Columnist Kevin Williams studied two most common production patterns for creating XSL style sheets: push and pull (PULL). He studied some simple XML and XSL examples and discussed the advantages and disadvantages of each method. When making an XSL style sheet, you can use any of these two main production patterns:
Push-style is constructed from a handler that creates an output in the output document. Outputs the type of element based on the source document. Element content is pushed to the appropriate handler. On the other hand, the pull style builds an output document as needed to pass the content in the source document.
The style you have chosen has an important impact on the code complexity and maintainability of the style sheet.
Pushing style table push sample table is the classic form of style sheet. All major XSL generating tools can generate a push-style sheet, which are commonly discussed in XSL reference materials. The style sheet in this format consists of a series of XSL: Template elements, each of which processing a specific element type in the document. For example, suppose you have the following source documents:
Listing 1. Sample Data Document
Kevin Williams Customername> 100 NOWHERE LANE address> nowheresville city> va state> 24182 zip> < Item> E38-19273 3-inch Grommets description> 37 count> 37.00 totalcost> item> E22 -18272 id> 2-inch widgets description> 22 count> 11.00 totalcost> item> invoice> john public < / Customername> 200 Anywhere Street address> anytown city> va state> 24177 zip> E22-18272 id> 2-Inch Widgets description> 27 count> 13.50 e19-28376 e19-28376 id> 1-inchem Bolts description> 16 count> 4.00 totalcost> item> invoice> orderrs> Now, you want to convert it to the following XHTML document:
Listing 2. Sample data output
kevin williams h1>
100 NOWHERE LANE NOWHERESVILLE, VA 24182
Description < / TH>
COST TH>
37.00
37.00 TD> TR>
2-Inch Widgets td>
11.00 td> tr> table>
john public h1>
200 Anywhere Street Anytown, VA 24177 P>
Description th>
COST TH> TR>
2-Inch Widgets TD>
13.50 TD> tr>
1-Inch Bolts TD>
4.00 TD> TD> Table> body> html> You need to create a series in the style sheet template. These templates handle each of the different elements that you want to bring into the output document. Each template must also create supported XHTML element structures, such as table headings and rows. Use the push method from the source document (Listing 1) to create the output of the output shown in Listing 2 looks like the following:
td> xsl: template> xsl: template> xsl: stylesheet> Please note: There are two interested add items, you should They include in the style sheet:
The last empty template ensures that excess content (such as the unit price of the project on the invoice) from the output document. Template to handle all child elements in document order ensures that the processing is carried out from the beginning to the source. In that, you will not lose any invoice content just because there is no specific invoice template.
As you can see, this structure is a bit bloated. Although the production code is very simple, the maintenance code is tricky. Even for this very simple example, if you want to find out how the XHTML table is filled, you must jump from a template to another template to find the Create location of the TD object. In more complex examples, there may be a template for many pages, and many templates are called from other templates. The maintenance of the code is also more difficult.
The pull pattern table is on the other hand, and the pull-style table relies on the ability of the codent to know which elements in the desired source document appear. By repeating elements, the appropriate structure is created in the output and the value is pulled from the source document as needed, and the code can explicitly loop processing. Here is a style sheet that performs the same conversion as Listing 2, but it uses pull-up strategy instead of pushing policies:
Listing 4. Sample drawing style sheet
h1>
, xsl: text> xsl: text> p>
Description TH>
COST TH> Tr>
td>
td> tr> xsl: for-each> table> xsl: for-each> body> html> xsl: template> < / XSL: STYL Esheet> In this form of style, you have only one template that corresponds to the root you are trying to retrieve. Nested information embedded in the source document is retrieved by processing all sub-nodes of the currently specific type node by using the XSL: For-Each element. As you can see, this style sheet is very easy to read. It is shorter than the form of pitched style, and its layout is very close to the expected output document layout. When encoding in the form of a draw, it is added to the top-level template as long as the desired output sample is employed. The value in the sample can then replace the value in the sample in the sample to retrieve the value of the source document.
Therefore, pull is always better than pushing, right? Although the format is indeed better than pushing format (readability, concise, and easy to read layout), you should also choose a style sheet based on the features of the source document. Check out the following structure example, which contains a narrative describing a fictional medical process: Listing 5. Sample Narrative Document The Surgeon Then Clealed and Dressed The Incision Procedure>, Using Four two-by-two sterile gauze pads Tool> and Medium Sutures Tool>. section> narrative> surgeylog>
In this narrative, information may appear in any order. One line in the document may have two TOOL clauses, followed by a procedure clause, or three Location clauses. Suppose you want to produce an output similar to the following:
Listing 6. Sample narrative output
surgery log h1>
13:00:00 b> p>
first, the surgeon buy the < I> 3-Inch Scalpel i> To make a Two-inch incision font> in the pattern's limited l l. cleaned and dressed the incision font>, using Four Two-by-two sterile gauze pad i> and medium SUTURES i>. body> html> related styles A very important thing in the form of format is that the order of the information is not related. The following style sheet processes the source XML in Listing 5 into a desired XHTML:
Listing 7. Narrative sample push table
surgery log h1> body> html> xsl: template> < P> b> p> i> xsl: template> First, The Surgeon Used The 3-Inch Scalpel Tool> To Make a two-inch incision procedure> in the patient's lower left abdomen location>. Cautery procedure> was then applied to the severed blood vessels target> to stop the bleeding . section> The Surgeon Then Cleaned and Dressed The Incision Procedure>, Using Four Two-by-Two Sterile Gauze Pads TOOL> And Medium SUTURES Tool>. section> Narrative> SURGERYLOG> When the style table is applied to the source document, the result is as expected: the tool embedded in the process is a bevel and is Orange. Now, look at the same style sheet created with pull-up style:
Listing 9. Narrative sample pull style table
Surgery log h1>
B> p>
font> xsl: by> xsl: Otherwise> xsl : choose> xsl: for-each> p> xsl: for-each> body> html> xsl: template> xsl: styleSheet> Please note that this pull-up style is bloated. You have to check each node and - if the node needs special processing - add the appropriate format to it. This style sheet does not even handle the complex situation quoted in Listing 8. This code ignores the tool (TOOL) tag within the process (the procedure) tag. If you extend the style sheet to properly handle all possible tools, targets, procedures, etc., the style table will quickly expand to an uncontrolled (and unmatched) size.
Conclusion These example demonstrates the main points I have drawn in most columns: Data documentation and narrative files are essentially different because:
The information in the data document appears in the order that can be expected in the code. The data document does not contain loose text, so the text is converted into an output document without a special code. To reorder them, reordering them and aggregate one level (or push them down to a more detailed level), which is not complicated to the output document. If you use a pull model, the mapping is simple and easy to maintain.
On the other hand, the document based on the narrative is completely opposite.
Information appears in the order that cannot be easily foreseen. Some text is free from the context of the element, so it is necessary to move it correctly to the output document.
In order to accurately reproduce the description in the output document, the style sheet must be handled regardless of the elements, and the push mold is comparable to this aspect.
Briefly, when designing a pattern of data documents, first consider using the pull model. For the narrative document, if possible, use the push model. More popular in writing these two style sheets - and know their respective advantages and disadvantages - will allow you to handle any style sheet design work that you may encounter in the future.
Reference
Participate in the Forum on this article by clicking the discussion at the top or bottom of this article. Learn a good basis knowledge of XSLT and XPath technology from W3C.
Please read Kevin Previous column and article: XML for Data # 1: Using XML Schema Prototype (DeveloperWorks, June 2001) XML For Data # 2: Model Style (DeveloperWorks, Ju 201 Ju 201) XML for Data # 3 : XLINK and Data (DeveloperWorks, July 2001) XML For Data # 4: Flexible Architecture 4 Skills (DeveloperWorks, Aug 2001) XML For Data # 5: Native-XML Database: A bad idea for data ? (DeveloperWorks, October 2001) XML For Data # 6: Multi-to-many relationship model (developerWorks, Jan 2002) XML For Data # 7: For XQuery's forward (developerWorks, February 2002) SOAPBOX: Kevin Telling him to advocate why it is in terms of data, XML Schema does not breathe the power of DTD (DeveloperWorks, June 2001).
Read XML Structures for Existing Databases, from a section of the Wrox Publishing Professional XML Databases (DeveloperWorks, January 2001).
Get the IBM WebSphere Studio Site Developer, which is built, test, and deploy Java Server Pages, servlets, and an easy-to-use integrated development environment for applications related to XML.
Issue how to be an IBM certified developer in XML and related art.
Please find more reference materials about XML on the developerWorks XML zone.