XML: XSL style sheet for data: Push or pull?

zhaozj2021-02-11  172

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

100 NOWHERE LANE nowheresville va 24182 < Item> E38-19273 3-inch Grommets 37 37.00 E22 -18272 2-inch widgets 22 11.00 john public < / Customername>
200 Anywhere Street anytown va 24177 E22-18272 2-Inch Widgets 27 13.50 e19-28376 e19-28376 1-inchem Bolts 16 4.00 Now, you want to convert it to the following XHTML document:

Listing 2. Sample data output

kevin williams

100 NOWHERE LANE
NOWHERESVILLE, VA 24182

Description < / TH> COST 37.00 37.00
2-Inch Widgets 11.00

john public

200 Anywhere Street
Anytown, VA 24177

Description COST
2-Inch Widgets 13.50
1-Inch Bolts 4.00 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:

Listing 3. Sample Sample Table

Description Cost


< XSL: Value-of select = "." /> ,

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


,

Description COST

< / 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 , Using Four two-by-two sterile gauze pads and Medium Sutures .

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

13:00:00

first, the surgeon buy the < I> 3-Inch Scalpel To make a Two-inch incision in the pattern's limited l l. cleaned and dressed the incision , using Four Two-by-two sterile gauze pad and medium SUTURES . 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 < P> First, The Surgeon Used The 3-Inch Scalpel To Make a two-inch incision in the patient's lower left abdomen . Cautery was then applied to the severed blood vessels to stop the bleeding .
The Surgeon Then Cleaned and Dressed The Incision , Using Four Two-by-Two Sterile Gauze Pads And Medium SUTURES . 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

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.

转载请注明原文地址:https://www.9cbs.com/read-5135.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.029, SQL: 9