Simple XSLT tutorial-an example of XSLT

Source: Internet
Author: User
Tags xsl xsl file xslt xslt example xslt processor
How to convert XML into XSLT

Let's make an interesting example. You have played the Plasticine and press it on different models to make the desired shape. If we assume that the XML data document is a big plasticine, XSLT is like a model. With one click, we can make the desired shape-HTML documents meeting different needs.

Take a look at the following process:

We input the original XML document, use XSL as the template, and use the conversion engine to output the required HTML document. The conversion engine is a metaphor for the process of "force-per-click. In a specific application, there is a special software to implement this conversion process, called XML processor. At present, there are many processor software (as described below), and XML processor has been embedded in ie5.5.

One instance

Now let's look at a simple XSLT Application Example to get some sensory knowledge. Many Web designers can feel at ease when they see HTML-like code. The code is so friendly and familiar.

Example 1: "Hello, world! "

Hello world, as the first tutorial, is already a convention in programming languages. We also follow this Convention to see how to use XSLT to display "Hello World ". Although this example has no practical purpose, please do not worry. There are more detailed examples later.

Step 1: Create the hello. xml file to be entered.

<?xml version="1.0" encoding="iso-8859-1"?><greeting>Hello, world!</greeting>

This is a simple XML document that contains only one node's XML structure tree.

Step 2: Create the XSLT document hello. XSL. Tip: the default extension of the XSLT file is. XSL.

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"><xsl:template match="/">

Now you can open the hello. XSL file in ie5.0 or a later browser and see the structure tree of XSL.

Step 3: Call the XSL file in XML. Modify the hello. XML code:

<?xml version="1.0" encoding="iso-8859-1"?><?xml-stylesheet type="text/xsl" href="hello.xsl"?><greeting>Hello, world!</greeting>

OK, all the code has been completed in this step. Next, you only need to use an XSLT processor (XML processor) to execute hello. XML shows the result of "Hello World. Popular processor software includes the following types:

  • James Clark's XT. Download URL: http://www.jclark.com/xml/xt.html
  • The ibm xml for Java software package named Lotus XSL. Download URL: www.alphaworks.ibm.com/tech/xml4j
  • Saxon. Download URL: http://www.wrox.com
  • Microsoft's msxml3. Download URL: http://www.microsoft.com/xml

Some netizens asked me, how should I see "Hello World" in my browser? The msxml3 interpreter is embedded in Microsoft ie5.5. You can use ie5.5 to open the hello. xml file and see the result. If you only see the XML structure tree, rather than the "Hello World" text, it means that msxml3 is not installed in your browser.

What should I do if I want to see the results without installation? We still use the old method in the XML tutorial to implement it using Js. (This is beyond the scope of this article, but we provide the instance code here to make it more intuitive and easy to understand .) The following is a real code, which can be saved as hello.htm and put in the same directory as hello. xml and hello. XSL above. Use ie5.0to open hello.htm in the upper-right corner to see the effect.

Process Analysis

If you can see the result successfully, you may want to know the specific meaning of the Code. Let's explain in detail the hello. XSL file.

<? XML version = "1.0" encoding = "iso-8859-1"?> This is the first line of code for the standard XML document, because XSLT itself is also an XML document. The encoding attribute is used to define the encoding format used by the document, and the iso-8859-1 mainly supports language encoding in Western Europe and North America. If you want to use simplified Chinese, you should write it as follows:<? XML version = "1.0" encoding = "gb2312"?>The subsequent code is: <XSL: stylesheetxmlns: XSL = "http://www.w3.org/1999/XSL/Transform" version = "1.0"> This is the first line of the standard XSLT file. XSL: stylesheet Code refers to processing a document as a style sheet. Xmlns: the XSL attribute is a namespace declaration, which is used in the same way as the namespace in XML to prevent repeated and chaotic element names. The prefix XSL indicates that the elements used in the document comply with W3C XSLT specifications. The last version attribute indicates that the style sheet only adopts the standard function of XSLT 1.0, which is the only standard currently. <XSL: template match = "/"> An <XSL: Template> element defines a template rule. Attribute match = "/" indicates the starting point of the template rule in the XML source document. "/" Is an XPATH syntax, which will be detailed later. "/" here represents the root of the XML structure tree ). The subsequent code is: <HTML> 

Okay. The example is explained. Have you ever wondered why to display "Hello World" in such a complicated way? The key lies in the fact that Hello world can be extracted from XML documents and processed using different XSLT templates to output documents with different requirements. Let's take a look at the main uses of XSLT:

Usage of XSLT

XSLT is mainly used for data conversion applications.

XML-based e-commerce is widely used, and XSLT is becoming increasingly important for data conversion. For example, you can directly convert the data format of TV news into the data format required by newspaper News, convert the stock data into images and display them on the webpage, and perform statistics on EDI (Electronic Data Exchange) data, sort and so on.

XSLT is an ideal tool for processing similar jobs.

 

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.