[Getting Started] XSLT Chapter 2: XSLT instances

Source: Internet
Author: User
Tags xsl xsl file xslt xslt example xslt processor
Chapter 2: XSLT instances

ArticleAuthor: A Jie

. 1 how to convert XML into XSLT
2.2 one instance
2.3 Process Analysis
2.4 usage of XSLT

2.1 How XSLT converts XML

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.

2.2 one instance

Now let's look at a simple XSLT Application Example to get some sensory knowledge. Many Web Designers see HTML-likeCodeThe code is so friendly and familiar.

Example 1: "Hello, world! "

Hello world is already the first tutorialProgramThe conventions in the language. 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 = "/">

<HTML>

<Head>

<Title> first XSLT example </title>

</Head>

<Body>

<P> <XSL: value-of select = "greeting"/> </P>

</Body>

</Html>

</XSL: Template>

</XSL: stylesheet>

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 and you will see "Hello
World. Popular processor software includes the following types:

(1). James Clark's XT. Download URL: http://www.jclark.com/xml/xt.html

(2). IBM's XML for Java package, named Lotus XSL. Download URL: www.alphaworks.ibm.com/tech/xml4j

(3). Saxon. Download URL: http://www.wrox.com

(4). 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, it is not a separate "hello"
The word "world" indicates 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.

<HTML>

<Head>

<Script language = "JavaScript" for = "window" event = "onLoad">

VaR xmldoc = new activexobject ("Microsoft. xmldom ");

Xmldoc. async = "false ";

Xmldoc. Load ("Hello. xml ");

Nodes = xmldoc.doc umentelement. childnodes;

Greeting. innertext = nodes. Item (0). text;

</SCRIPT>

<Title> first XSLT example </title>

</Head>

<Body bgcolor = "# ffffff">

<Span id = "greeting"> </span> <br>

</Body>

</Html>

2.3 Process Analysis

If you successfully see the results, you may want to know the specific meaning of the Code. Let's explain in detail: Look at 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 following code is as follows:

<XSL: stylesheet

Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"

Version = "1.0">

This is the first line of code in 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 uses XSLT.
1.0 of standard functions, 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 following code is as follows:

<HTML>

<Head>

<Title> first XSLT example </title>

</Head>

<Body>

<P> <XSL: value-of select = "greeting"/> </P>

</Body>

</Html>

Note: When a template rule is triggered, the template content controls the output results. In this example, most of the template content consists of HTML elements and text. Only the <XSL: value-of> element is the XSLT syntax. Here <XSL: value-of> is used to copy the values of a node in the original document to the output document. The select attribute specifies the node name to be processed in detail. This is the XPath syntax. "greeting" means to find the element whose root node name is greeting and process the node with a template. Find the <greeting> element, and set the value of the element "hello ".
World "is copied to the output file according to the template style.

Tip: Because the XML document is strictly hierarchical (you can view the XML file in ie5 and see the XML document similar to the Multi-Level Association menu), the XML document in our image is called the document tree, each pair of elements is called a node of the tree. The root element is the root node.

 

Finally, close all elements:

</XSL: Template>

</XSL: stylesheet>

 

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:

 

2.4 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.