What kind of language is XSLT?

Source: Internet
Author: User
Tags abstract end expression final sql version variable software ag

Analysis and overview

Content:


What is XSLT?
Role of XSLT
XSLT as a language
Operation of the XSLT processor
Sample style Sheet
The advantages of XSLT
Conclusion
Resources
About the author
Evaluation of this article


What kind of language is XSLT, what is its purpose, and why is it designed like this? There are many different answers to these questions, and beginners tend to be confused because there is a big difference between the language and the language they used to use. This article attempts to illustrate XSLT. This article does not attempt to teach you to write XSLT style sheets, which will explain the origins of the language, what it is good at, and why you should use it.
The purpose of this article is to provide the necessary background for a technical article on Saxon, and to provide insider information on the implementation techniques used in traditional XSLT processors to help users maximize the performance of their style sheets. But DeveloperWorks's editors persuaded me that this presentation should appeal to a wider audience, and that it deserves to be published separately as an independent description of the XSLT language.

What is XSLT?
The XSLT language is defined by the World Wide Web Consortium (WWW), and version 1.0 of the language is published as a "recommendation" on November 16, 1999 (see Resources). I have provided a comprehensive specification and user Guide in the Flops XSLT programmers ' Reference, so I'm not going to cover the same content in this article. Specifically, the purpose of this article is simply to give the reader an idea of where XSLT fits on a large scale.

Role of XSLT
The original purpose of XSLT is to separate the content of the information from the WEB display. As originally defined, HTML implements device independence by defining the display by abstract concepts such as paragraph, focus, and numbered lists. As the Web becomes more and more commercial, publishers want to have the same quality of output as print. This gradually leads to more and more use of concrete display controls, such as the explicit font and absolute position of material on the page. Unfortunately, however, the side effects can be expected, and the same content will become increasingly difficult to pass on to alternative devices, such as digital televisions and WAP phones, which are reproduced in the jargon of the printing industry.

Having absorbed the experience of using SGML in the printing industry, a markup language XML was defined in early 1998 to represent structured content that is independent of the display. Unlike HTML, which uses a set of fixed concepts, such as paragraphs, lists, and tables, the tags used in XML tags are entirely user-defined, with the intention that they should be related to objects of concern, such as people, places, prices, and dates. Although elements in HTML are essentially print styles (although at an abstract level), the goal of XML is that elements should describe the actual object. For example, listing 1 shows an XML document that represents the results of a soccer tournament.

Listing 1. An XML document that represents the results of a soccer tournament
<results group= "A" >
<match>
<date>10-Jun-1998</date>
<team score= "2" >Brazil</team>
<team score= "1" >Scotland</team>
</match>
<match>
<date>10-Jun-1998</date>
<team score= "2" >Morocco</team>
<team score= "2" >Norway</team>
</match>
<match>
<date>16-Jun-1998</date>
<team score= "1" >Scotland</team>
<team score= "1" >Norway</team>
</match>
<match>
<date>16-Jun-1998</date>
<team score= "3" >Brazil</team>
<team score= "0" >Morocco</team>
</match>
<match>
<date>23-Jun-1998</date>
<team score= "1" >Brazil</team>
<team score= "2" >Norway</team>
</match>
<match>
<date>23-Jun-1998</date>
<team score= "0" >Scotland</team>
<team score= "3" >Morocco</team>
</match>
</results>




If you want to display the results of these football matches through a Web browser, do not expect the system to produce a reasonable layout. Other mechanisms are needed to tell the system how to display data on a browser screen, TV, WAP phone, or really on paper. This is the purpose of using style sheets. A style sheet is a set of descriptive rules that define how information elements of a tag identity in a source document should be represented.

The consortium has defined the stylesheet standard for two series. The first is a CSS (cascading style sheet) that is widely used in HTML, but it can also be used in XML. For example, you can use CSS to indicate when an invoice is displayed, and the total amount to be paid should be shown in bold 16-point Helvetica. However, CSS cannot perform calculations, rearrange or sort data, combine data from multiple sources, or personalize what is displayed based on user or session characteristics. In the case of the results of the football match, the CSS language (even the latest version CSS2, not fully implemented in the product) is not strong enough to handle this task. For these reasons, the consortium has embarked on the development of more powerful stylesheet language XSL (Extensible Stylesheet Language) and incorporates many good ideas from the Dsssl (document style, semantic, and canonical language) developed in the SGML community.

During the development of the XSL (which has been foreshadowed in Dsssl), it is found that the tasks performed during the preparation of the XML document for display can be divided into two phases: transformation and formatting. A transformation is the process of converting an XML document (or its in-memory notation) into another XML document. A format is the process of converting a converted tree into a two-d graphical representation or possibly a one-dimensional audio stream. XSLT is a language developed to control the first phase of "transformations". The second stage of "format" development work is still in progress. But in fact, most people now use XSL to convert XML documents into HTML and use HTML browsers as the format engine. This is possible, because HTML is actually just an example of an XML glossary, and XSLT can use any XML vocabulary as its goal.

The separation of the two operations into one language and the other is a good decision, since many applications that convert languages prove unable to display documents to the user. As XML is increasingly used as a data interchange syntax in E-commerce, the need for applications to convert data from one XML vocabulary to another is increasing. For example, an application might extract the details of a TV program from the TV Program Guide and insert them into the monthly billing of a paid customer. Similarly, there are a number of useful data transformations in which the source glossary and the target glossary are the same. They include data filtering, as well as business operations, such as the imposition of price increases. Thus, as more and more of the data is being used in the form of XML syntax in the system, XSLT is becoming a ubiquitous high-level language for processing these data.

In flops, I do a metaphor: the relationship between XSLT and XML is like the relationship between SQL and tabular data. The powerful function of a relational model comes not from the idea of storing data in a table, but rather from the advanced data operations that are feasible in SQL based on relational operations. Similarly, the hierarchical data model of XML is actually very small to help application developers. It is precisely because XSLT provides such a powerful function as the advanced operational language of XML data.

XSLT as a language
In some ways, XSLT is very strange as a language. I do not intend to discuss in this article the rationale for the design decisions that have been made, although they can be logically traced back to the requirements of XSLT as determined by the language designer. For a more complete description, please refer to Chapter 1th of flops.

The following outlines some of the main features of the XSLT language.

An XSLT style sheet is an XML document. Represents the structure of a document by using the angle bracket notation syntax for XML. This syntax is somewhat awkward, and this decision can make the language more verbose. But it does have its benefits. It represents all vocabulary devices that can automatically use XML (for example, Unicode character encoding and escaping, use of external entities, and so on). It indicates that it is easy to make an XSLT stylesheet into the input or output of a transformation so that the language can be used for itself. It also makes it easy to embed the expected XML output block into the stylesheet. In fact, many simple stylesheets can basically write the template that expects the output document, and you can embed some special instructions in the text to insert the variable data in the input or calculate a value. This makes XSLT very similar to many existing proprietary HTML template languages at this simple level.

The basic processing paradigm is pattern matching. In this respect, XSLT inherits the tradition of text-processing languages such as Perl, which can be traced back to the language of the 1960 's, such as Snobol. The XSLT style sheet includes a set of template rules, each of which uses the following: "If this condition is encountered in the input, the following output is generated." "The order of the rules is irrelevant, and when there are several rules that match the same input, the conflict resolution algorithm is applied." However, the difference between XSLT and a serial text processing language is that XSLT does not process the input line by row. In fact, XSLT treats the input XML document as a tree structure, and each template rule applies to one node in the tree. The template rule itself determines which nodes to process next, so you do not have to scan the input in the original order of the input document.

Operation of the XSLT processor
The XSLT processor uses the tree structure as its input and generates another tree as output. The point is shown in Figure 1.

Figure 1. The tree structure of XSLT input and output


The input tree structure is often generated by parsing the XML document, and the output tree structure is often serialized into another XML document. However, the XSLT processor itself operates in a tree structure rather than an XML character stream. This concept initially gives many users a sense of unreality, but the result is crucial to understanding how to perform more complex transformations. First, it means that the XSLT processor can understand the special place in the source document that has nothing to do with the tree structure. For example, whether a property is included in single quotes or in double quotes, it is not possible to apply different processing because both forms are considered different representations of the same basic document. In more depth, it means that processing an INPUT element or generating an output element is an atomic operation. It is not possible to separate the start and end tags of the processing elements into individual operations, because an element is automatically represented as a single node of the tree model.

XSLT uses the child language called XPath to refer to nodes in the input tree. XPath is essentially a query language that matches the hierarchical XML data model. It can select nodes by browsing the tree in any direction and apply predicates based on the value and position of the node. It also includes tools for basic string processing, numeric computations, and Boolean algebra. For example, an XPath expression ... /@title Select the Caption property of the parent element of the current node. XPath expressions are used to select input nodes to process, test conditions during condition processing, and calculate values to insert into the result tree. Template rules also use the simplified form "pattern" of XPath expressions to define which nodes a particular template rule applies to. XPath is defined in a separate consortium recommendation that allows you to use query languages that are reused in other contexts, especially for xpointer that define extended hyperlinks.

XSLT is based on the concept of functional programming in traditional languages such as Lisp, Haskell, and Scheme. Style sheets are made up of templates, which are essentially a single feature--each template defines part of the output tree as part of the input tree and does not have side effects. Rules that use no side effects are tightly controlled (except for escaping external code written in a Java-like language). The XSLT language allows you to define a variable, but not allow an existing variable to change its value-that is, there are no assignment statements. This strategy confuses many new users in order to allow the stylesheet to be applied incrementally. The rationale is that if the language has no side effects, making small changes to the input document, you do not have to perform the entire transformation from scratch to calculate the final change to the output document. It must now be said that it is theoretically possible that any existing XSLT processor is not yet implemented. (Note: Although XSLT is based on the concept of functional programming, it is not yet a complete functional programming language because it lacks the ability to handle functions as a primary data type.) )

Sample style Sheet
At this stage, the language becomes clearer using the example. Listing 2 shows a simple stylesheet that lists the results of a football match.

Listing 2. The basic style sheet of the football match result

<xsl:transform
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" version= "1.0" >

<xsl:template match= "Results" >
Results of Group <xsl:value-of select= "@group" >
</title><body>Results of Group <xsl:value-of select= "@group" >
<xsl:apply-templates>
</body></xsl:template>

<xsl:template match= "Match" >
<xsl:value-of select= "team[1]" > Versus <xsl:value-of select= "team[2]" >
<p>played on <xsl:value-of select= "date" ></p>
<p>result:
<xsl:value-of select= "team[1]" >
<xsl:value-of select= "team[1]/@score",
<xsl:value-of select= "team[2]" >
<xsl:value-of select= "team[2]/@score" >
</p>
</xsl:template>

</xsl:transform>





This stylesheet includes two template rules, one matching <results> element, and the other matching <match> element. The template rule for the <results> element outputs the title of the page and then invokes <xsl:apply-templates> this is an XSLT instruction that handles all the descendants of the current element and uses its appropriate template rules for each descendant. In this case, all descendants of the,<results> element are <match> elements, so the second template rule is used to process them. The rule outputs a secondary HTML title that identifies the match (in the form of "Brazil versus Scotland"), and then generates an HTML paragraph giving the date of the match and the score of the two teams.

The result of this conversion is an HTML document, which is represented in the browser as shown in Figure 2.

Figure 2. The results of the style sheet in Listing 2


This is a very simple way of representing information. However, the functionality of XSLT is much more powerful than this. Listing 3 contains another style sheet that can manipulate the same source data. This time, the stylesheet calculates a table of matches to show the rank of each team at the end of the tournament.

Listing 3. To calculate the style sheet for a team ranking table
<xsl:transform
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"
Version= "1.0" >


<xsl:variable name= "Teams" select= "//team[not (. =preceding::team)]" >
<xsl:variable name= "matches" select= "//match" >

<xsl:template match= "Results" >


<table cellpadding= "5" >
<tr>
<td>Team</td>
<td>Played</td>
<td>Won</td>
<td>Drawn</td>
<td>Lost</td>
<td>For</td>
<td>Against</td>
</tr>
<xsl:for-each select= "$teams" >
<xsl:variable name= "This" select= "." >
<xsl:variable name= "played" select= count ($matches [team= $this]) ">

<xsl:variable name= "won"
select= count ($matches [team[.= $this]/@score > team[.! = $this]/@score]) ">
<xsl:variable name= "Lost"
select= count ($matches [team[.= $this]/@score < team[.! = $this]/@score]) ">
<xsl:variable name= "Drawn"
select= count ($matches [team[.= $this]/@score = team[.! = $this]/@score]) ">
<xsl:variable name= "for"
select= "sum ($matches/team[.=current ()]/@score)" >
<xsl:variable name= "against"
select= "Sum" ($matches [Team=current ()]/team/@score)-$for >

<tr>
<td><xsl:value-of select= "." ></td>
<td><xsl:value-of select= "$played" ></td>
<td><xsl:value-of select= "$won" ></td>
<td><xsl:value-of select= "$drawn" ></td>
<td><xsl:value-of select= "$lost" ></td>
<td><xsl:value-of select= "$for" ></td>
<td><xsl:value-of select= "$against" ></td>
</tr>
</xsl:for-each>
</table>
</body></xsl:template>

</xsl:transform>





There is not enough space to fully describe the stylesheet, in short, it declares a variable for the team, and the value of the variable is a collection of nodes, each of which has an instance. It then calculates the total number of wins, flat or negative matches for each team, and the total number of goals or losses in the team. Figure 3 shows the final output of it in the browser.



Figure 3. Results of the rank style sheet in Listing 3


The purpose of this example is to show that XSLT is not only able to specify fonts and layouts for text that appears in the source document. It is a complete programming language that can transform source data for display in any way, or enter another application.

The advantages of XSLT
Why do you consider using XSLT?

XSLT gives you all the benefits of the traditional high-level declaration programming language, especially for the task of transforming XML documents.

The real benefit of advanced languages is the development of productivity. But in reality, the real value comes from the potential for change. XSLT applications used to transform XML data structures are more adaptable to changes to the details of XML documents than to procedural applications encoded with low-level DOM and SAX interfaces. In the database world, this feature is called data independence, precisely because data independence leads to the success of declarative languages such as SQL, and to the decay of old, guided data-access languages. I believe this is true in the XML world as well.

Of course, as with all declarative languages, XSLT degrades performance. But for most applications, the performance of today's XSLT processors is fully responsive to the needs of the application, and it's getting better. In my second article, I'll discuss some of the optimization techniques used in the XSLT processor, such as my own Saxon product.

Conclusion
What I want to show in this article is that XSLT is a complete high-level language for manipulating XML documents, just as SQL is a high-level language for manipulating relational tables. It should be noted that XSLT is not only a style design language, it is more powerful than CSS (or even CSS2).

I've seen applications where all of their business logic is encoded in XSLT. In a three-tier online banking system, I see:


Retrieves all data from the back-end operating system as an XML message.
During the duration of the session, the user's account data is represented in memory in the form of an XML DOM.
All information to the user is first encapsulated into an XML message and then converted to HTML using the browser's performance based on the XSLT transformation that accompanies the server or client.

The data for the application is in XML format, and logic (including data access logic, business logic, and display logic) is implemented by XSLT. I recommend that architecture for each project, but it will take a long time, and I think we'll see that system in a few years.

As a programming language, XSLT has many attributes-from its use of XML syntax to the basis of its functional programming principles-and is not familiar to ordinary Web programmers. That means a steep learning curve and usually encounters many setbacks. The same is true for SQL, all of which represent an essential difference between XSLT and previous programming languages. But do not give up: it is a very powerful technology, it is worth studying hard.

Resources

The same author writes the Wrox Book XSLT Programmer ' s Reference. A comprehensive guide to the XSLT language.
The XSLT 1.0 recommendation published by the Consortium. An authoritative specification for the XSLT language.
An XPath 1.0 recommendation published by the Consortium. An authoritative specification for XPath expression syntax used in XSLT style sheets.
Xsl-list, a busy mailing list about everything in XSLT, comes with searchable files that are managed by Mulberrytech.
Www.xslinfo.com, a good network Central page with links to XSLT resources, includes software, books, tutorials, and other content.
About the author
Michael Kay is very famous in the XML world, and he is the author of the Saxon XSLT processor and Wrox Book XSLT Programmer ' s Reference. Years ago, he earned his Ph. D., and his field of research was database technology. Since then, he has designed Codasyl databases, relational databases, object-oriented databases, and free text database software.

At the time of writing, Michael also had several jobs (and no rest). He has just ended his 24 job at ICL, a British IT service company, and defected to Software AG as part of the architecture team, which is responsible for controlling the direction of future XML products.

The author chooses this picture to prove that he is not always as serious as he is in Wrox books.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.