2. Example of XSLT
2.1 How XSLT converts XML
2.2 an instance
2.3 Process Analysis
2.4 Use of XSLT
2.1 How XSLT converts XML
We play a fun, you play the plasticine, press the different molds to make the shape of the need. If we assume that the XML data document is a big plasticine, XSLT is like a mold, and it is necessary to make a needed shape.
Specifically look at the following procedure:
We entered the XML original document, use XSL as a template, through the conversion engine, the final output required HTML document. The conversion engine is the process of "force one according to force" in the metaphor. In specific applications, there are special software to implement this conversion process, named XML Processor. There is already a lot of Processor software (hereinafter referred to in detail below), and XML Processor has been embedded in IE5.5.
2.2 an instance
Now let's look at a simple XSLT actual application example and get some sense of understanding. Many web designers see the code similar to HTML will be relieved, the code is kind and familiar.
Example 1: "Hello, World!"
Hello World as the first tutorial is already a practice in the program language. We also follow this practice to see how to use XSLT to display "Hello World". Although this example doesn't have any practical use, please don't worry, there is more detailed example.
Step 1: Create to enter the XML document Hello.xml.
Xml Version = "1.0" encoding = "ISO-8859-1"?>
This is a very simple XML document that contains only a node XML tree.
Step 2: Establish a XSLT document Hello.xsl. Tip: The default XSLT file has the name of .xsl.
Xml Version = "1.0" encoding = "ISO-8859-1"?>
hEAD>
body>
html>
xsl: template>
xsl: stylesheet>
You can now open this hello.xsl file with IE5.0 or above, see the XSL tree.
Step 3: Call this XSL file in XML. Modify the code for Hello.xml to:
Xml Version = "1.0" encoding = "ISO-8859-1"?>
xml-stylesheet type = "text / xsl" href = "hello.xsl"?>
OK, all the code has been completed by this step, followed by using a XSLT processor (XML processor) to perform Hello.xml, you can see the result of "HelloWorld". The popular processor software has the following:
(1) .james clars' XT. Download URL: http://www.jclark.com/xml/xt.html
(2). IBM's XML for Java 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 should ask, I want to see what the effect of quot; hello world "should be done? In Microsoft's IE5.5 embedded MSXML3 interpreter, you can open the hello.xml file with IE5.5, You can see the result. If you only see the XML tree, not a separate "Hello
World "words, indicating that your browser does not have MSXML3 version.
What should I do if I don't want to see it? Then use the old approach in our XML tutorial, use JS implementation. (This has exceeded the scope of this article, but in order to be more intuitive, we provide example code here.) The following is an implementation code that can be saved as hello.htm, Hello.xml, Hello .xsl is placed in the same directory. Finally, you can see the effect with IE5.0 or later.
VAR XMLDoc = New ActiveXObject ("Microsoft.xmLDom");
Xmldoc.async = "false";
XMLDoc.Load ("Hello.xml");
Nodes = xmldoc.documentelement.childnodes;
Greeting.innertext = nodes.Item (0) .text;
script>
hEAD>
span>
body>
html>
2.3 Process Analysis
If you see the effect, you may want to know the specific meaning of these codes, let's explain in detail: see the hello.xsl file
Xml Version = "1.0" encoding = "ISO-8859-1"?>
This is the first line code of the standard XML document, because the XSLT itself is also an XML document. Encoding properties are used to define the form of coding usage, ISO-8859-1 mainly supports Western Europe and North American language coding. If you want to use Simplified Chinese, you should write:
XML Version = "1.0" encoding = "GB2312"?>
The next code is: XMLns: XSL = "http://www.w3.org/1999/xsl/transform" Version = "1.0"> This is the standard XSLT file first line code. XSL: Stylesheet code means processing documents as a style sheet (STYLESHEET). XMLns: XSL attribute is a namespace declaration, as in XML, using methods in XML, to prevent element name repetition and confusion. Where the prefix XSL means that the elements used in the document abide by the W3C's XSLT specification. The last Version property Description Style Table is only using XSLT 1.0 standard function, this is also currently only standards. A The next code is:
hEAD>
body>
html>
Description: When the template rule is triggered, the content of the template will control the result of the output. In the example, most of the template is composed of HTML elements and text. Only the
World "Press template to copy to the output file.
Tip: Since the XML document is a strict hierarchy (view XML file with IE5, you will see the XML document similar to multi-level associated menu), so our image is called the XML document as a document tree, which is called a node of the tree. . The root element is root node.
Finally close all elements:
xsl: template>
xsl: stylesheet>
Ok, the example explained. Have you thought about why you want to use such a complex way to display "Hello World"?
The key is not on the surface, but in the substance: use this method, Hello World can be extracted from the XML document and use a variety of different XSLT template processing to output documents of different needs. Let's take a look at the main use of XSLT:
2.4 Use of XSLT
The main use of XSLT is data conversion applications.
Due to XML-based e-commerce, XSLT is also increasingly important as data conversion roles. For example, directly convert the data format of TV news into data format required for newspaper news; direct converting the stock data into a picture display on the web; statistics, sorting the EDI (electronic data exchange) data.
XSLT is an ideal tool for processing similar work.