Xsl template definition




















Perhaps the output is targeted to a visually-impaired audience and so the graphical image is suppressed and the text presented differently for an audio reader. Consider in the next diagram how the importing stylesheet augments the existing handling for the node identified as "2" as the imported stylesheet shown in the earlier diagram.

Now when the result is built, three nodes are added to the tree for each of the four input nodes, one being new and the other two being the original output. This illustrates how to augment the output with additional information while not touching the original stylesheet. Perhaps this output is targeted to editorial staff who need to see important information hidden from the main audience but critical to the task of reviewing your XML document content being published. By wrapping the stylesheet for the main audience with two different stylesheets for two other different audiences, the bulk of the work in the main stylesheet does not have to be maintained separately.

The cost of developing the main stylesheet gets leveraged for the new audiences with little relative effort. Compare this approach with what would be required in imperative languages and the different assembly and maintenance of imperative fragments. An interesting opportunity is to implement an 11th-hour production fix to a read-only stylesheet that perhaps is locked in to a source code control system.

The importing stylesheet supplants the undesirable processing with the required processing until the required processing can be migrated into the base stylesheet. Using these techniques a project team can create libraries of debugged and working stylesheets that are to be pulled into and leveraged in multiple solutions.

Fragments can be overridden or augmented, but the side-effect-free nature of the language guarantees the fragments cannot be "broken" by the influence of other templates. This is the essence of the US intelligence document publishing system cited earlier: the imported stylesheets provide the common base behaviours while the importing stylesheets provide the agency-specific appearances and augmentations, all done in a robust fashion enabling quick new deployments and system-wide maintenance.

Moreover, the maintenance experience across multiple XSLT projects has been testament to the ease of adding new functionality without disturbing existing functionality. While XQuery does not offer the same level of polymorphism of common code fragments, the imperative nature is very attractive to traditional programmers. One might consider, then, using XQuery in the data flow when assembling content and using XSLT in the data flow when publishing content.

This would be depicted as follows. The diagram shows two wrapper XSLT stylesheets importing the same collection of imported stylesheets to produce distinctive results from the one structured input created by the XQuery interpretation of projected data. The opposite, however, of trying to deploy XQuery for the leveraged common code polymorphism available in XSLT would be a programming and maintenance challenge.

The basic choices are running the transformation directly in the user agent, or running the transformation on the server, either statically in advance of to the connection from the browser or dynamically in response to browser interaction. It is important to realize, however, that there are two ways in which XSLT can be run in the browser user agent.

Many browsers offer only XSLT 1. Different choices of XML processor and different choices of XSLT processor result in differing and inconsistent results between browser implementations. Provided that JavaScript is not disabled, all modern web browsers execute the engine consistently to produce identical results for all users. One interprets an XML document describing how information is to be processed and outputs the XSLT stylesheet implementing the processing so described.

This is depicted as follows. This is not simply academic. The output of executing that generated XSLT logic against the production data is the validation report.

These two simple examples are excerpted from the five hours of free preview of the hour streaming XSLT hands-on training class on Udemy. This is the only code included in this article and the concepts in this working code are greatly simplified to illustrate the contrast between the imperative and declarative programming styles when writing XSLT. I've limited the examples to XSLT 1. This XML document is structured not around any particular presentation.

Rather, the XML is structured around the inherent relationships of the information expressed in a tree. Lines carry all product information. Lines carry the record of all product sales to customers, organized by customer.

Now let's consider two HTML presentations of that information as two separate transformations shown in two browser windows where on the left is a tabular presentation of the information, and on the right is a list presentation of the same content.

We need to write the stylesheet that builds from the XML input to an HTML output with elements and attributes expressing HTML semantics of presentation, because browsers do not recognize the sales-oriented elements used in the input. The information doesn't change in this example, only the arrangement of the information and the elements used in that new order.

Consider first using XSLT in an imperative manner, much like one would write XQuery, where the entire self-contained expression is a single template. It is critical to understand that in XSLT we are manifesting the output from within the stylesheet expression, not using function calls to synthesize elements and attributes out of thin air though this is available and sometimes it is the most expedient way of dynamically calculating the output information.

Just its presence in the stylesheet is enough to create the output. In fact, this is the nub of XSLT: the stylesheet manifests the output in the form of a template that is triggered by and fleshed out with the input information.

In the imperative example, the entire stylesheet is the template of the result tree. Within every template, large or small, the elements not in the XSLT namespace are used directly in the result, while the elements in the XSLT namespace are instructions that are interpreted as control constructs or action directives obtaining or assembling information to be included under and beside the manifest result elements.

This empty table header is the blank cell at the start of the heading line. Thus, the second and subsequent cells of the heading line contain all of the product names. Line 15 shows the binding of the current node to a variable named "customer". Lines 18 and 19 have an XPath address finding the appropriate information to be included in the report.

Finally lines 23 through 28 build the totals row, including a cell for the word "Totals" and using on line 27 a built-in function to sum all of the cells addressed by the stylesheet. The programmer doesn't have to implement the summation logic, it is enough to simply ask the processor to sum the values of the addressed nodes. Processing begins by building the result tree using the template of the template rule for the root node declared in lines 6 through Contrast the two solutions by the isolation of the templates.

There is but one template in the imperative solution and so that stylesheet can be used to create only one result. There are three templates in the declarative solution and so that stylesheet can be leveraged to produce multiple results by having the templates overridden by multiple importing stylesheets.

And any logic in any of the three templates has no influence on any of the other templates, so maintaining a template will not break others. The expression language of XSLT offers a number of benefits over XQuery and other imperative languages, primarily in the polymorphism of transformation behaviours for creating differing but related solutions.

While the language requires training and practice in the art of creating nuanced and powerful stylesheet fragments, the investment is returned in the integrity, flexibility and robustness of the stylesheets deployed. Choosing to use XSLT is important to your information processing planning for XML and other structured content such as is found in databases.

It is worth the effort to deploy properly. January 1, G. Ken Holman xslt xquery. Table of Contents 1. The task at hand 2. How XSLT is different from other programming languages 3. Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy.

By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. XSLT Select. Free Software Development Course. Login details for this Free course will be emailed to you.

Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. Report Error. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.



0コメント

  • 1000 / 1000