2. Examples of XSLT
2.1 How XSLT Transforms XML
2.2 An instance
2.3 Process Parsing
The purpose of 2.4 XSLT
2.1 How XSLT Transforms XML
Let's make an interesting analogy: you play with the dough, you press it in different molds, you can make the shape you need. If we assume that the XML data document is a big piece of dough, XSLT is like a mold, pressing it, making the desired shape to---the HTML document that fits the different needs.
Look at the following process diagram:
We input the original XML document, using XSL as the template, through the transformation engine, the final output of the required HTML document. The conversion engine is the metaphor of the "Force One press" process. In the specific application, there is a special software to implement this transformation process, called XML Processor. There are already a lot of Processor software (detailed below), and XML Processor is already embedded in IE5.5.
2.2 An instance
Now let's take a look at a simple XSLT practical application example to get some sense of perception. Many web designers see HTML-like code to be assured that the code is so cordial and familiar.
Example 1: "Hello, world!"
Hello World as the first tutorial is already a practice in the programming language. We also follow this practice and see how XSLT can be used to show "Hello World". Although this example has no practical use, please do not worry, there are more detailed examples.
Step one: Create Hello.xml to enter XML document.
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<greeting>hello, world!</greeting>
This is a very simple XML document that contains only one node of the XML Structure tree.
Step two: Create an XSLT document HELLO.XSL. Tip: The default XSLT file has a suffix named. Xsl.
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<title>first XSLT example</title>
<body>
<p><xsl:value-of select= "Greeting"/></p>
</body>
</xsl:template>
</xsl:stylesheet>
You can now open the hello.xsl file in IE5.0 above browser and see the XSL structure tree.
Step three: Call this XSL file in XML. The code to modify Hello.xml is:
<?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, and the next thing you can do with an XSLT processor (XML Processor) is to see the hello.xml "Hello
World "show the results. Popular processor software is available in the following ways:
(1). James Clark's XT. Download URL: http://www.jclark.com/xml/xt.html
(2). IBM's XML for Java software package, named Lotusxsl. 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 want to ask, I want to see in the browser "Hello World" effect how should do? With the MSXML3 interpreter embedded in Microsoft's IE5.5, you can use IE5.5 to open hello.xml files and see the results. If you see only the XML Structure tree, not the individual "hello
World ", stating that your browser does not have the MSXML3 version installed.
What if there is no installation and want to see the effect? It is still used in our XML tutorial in the old ways, using JS implementation. (This is beyond the scope of this article, but in order to be more intuitive and easy to understand, we provide instance code here.) Below is an implementation code that can be saved as hello.htm and placed under the same directory as the hello.xml,hello.xsl above. Finally use IE5.0 above version to open hello.htm can see the effect.
<script language= "JavaScript" for= "window" event= "onload" >
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.async= "false";
Xmldoc.load ("Hello.xml");
nodes = XmlDoc.documentElement.childNodes;
Greeting.innertext = Nodes.item (0). text;
</script>
<title>first XSLT example</title>
<body bgcolor= "#FFFFFF" >
<span id= "Greeting" ></span><br>
</body>
2.3 Process Parsing
If you successfully see the effect, you may want to know the specific meaning of these codes, let us 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 a standard XML document, because XSLT itself is also an XML document. The encoding attribute is used to define the encoding used for the document, and iso-8859-1 mainly supports language encoding in Western Europe and North America. If you want to use Simplified Chinese, then you should write:
<?xml version= "1.0" encoding= "GB2312"?>
The next code is:
<xsl:stylesheet
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"
Version= "1.0" >
This is the first line of code for the standard XSLT file. The Xsl:stylesheet code means that the document is processed as a stylesheet (stylesheet). The Xmlns:xsl property is a namespace declaration, which, like the namespace usage method in XML, is used to prevent element name duplication and confusion. Where prefix xsl means that the elements used in the document conform to the XSLT specification of the consortium. The final version attribute shows that the stylesheet only uses XSLT
1.0 standard functionality, which is currently the only standard.
<xsl:template match= "/" >
A <xsl:template> element defines a template rule. The attribute Match= "/" describes the starting point of the template rule action in the XML source document. "/" is an XPath syntax, which we'll cover in more detail, where "/" represents the root of the XML structure tree.
The next code is:
<title>first XSLT example</title>
<body>
<p><xsl:value-of select= "Greeting"/></p>
</body>
Description: When the template rules are triggered, the contents of the template control the output results. In the example, most of the template content consists of HTML elements and text. Only the <xsl:value-of> element is the XSLT syntax, where the function of <xsl:value-of> is to copy the value of one node in the original document to the output document. The Select property specifies the node name to be processed in detail. This is the XPath syntax, and "greeting" means to look for elements with the root node named greeting and to process the node with a template. The specific is to find the <greeting> element, and then the element's value "Hello"
World "Copy to output file by template style.
Tip: Because the XML document is a strict hierarchy (see XML Files with IE5, there are similar multilevel associative menus in XML documents), we image the XML document as a document tree, where 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, here's the illustration. Have you ever wondered why you should use such a complex method to show "Hello World"?
The key is not the surface, but the substance: in this way, Hello World can be extracted from XML documents and processed by various XSLT templates to output documents of different requirements. Let's look at the main uses of XSLT:
The purpose of 2.4 XSLT
The main purpose of XSLT is Data transformation applications.
Because of the widespread popularization of xml-based E-business, XSLT is becoming more and more important as the role of data transformation. For example, directly convert the data format of TV news to the data format required by the newspaper news, transfer the stock data directly to the picture displayed on the Web page, and the EDI (Electronic data Interchange) data to be counted, sorted and so on.
XSLT is an ideal tool for dealing with similar work.