XSLT Getting Started - Chapter 1 Getting Started: Leading beginners to start XSLT travel [transfer]

xiaoxiao2021-03-06  60

Directory - 1 Foreword - 2 First Step - 3 Conclusion

1 Introduction

In general, there is no style information (such as font, etc.) in the XML document. We must specify style information to display an XML document in an appropriate form. XSL (Extensible Stylesheet Language) is a style description language that can be applied in an XML document.

XSL has two types.

XSLT (XSL Transformations)

Convert an XML document into other XML documents

FO (Formatting Objects)

Define the reading method of the document

The standard of FO has not been determined. The version version 1.0 is the standard recommended by the W3C. Since the XSLT is an XML document conversion language, although it cannot directly define the display mode, it can be converted to the displayed format by converting the XML document to the designated style. In the future, it may be possible to convert XML into easy display formats via XSLT, and the XML can be converted to the definition of reading methods via FO.

Although it is an XML document conversion, since the purpose of XSLT is to specify the style, it is not possible to say that it is a complete universal conversion language. However, XSLT can be used in many cases of XML document conversion.

2 first step

Let us first look at a simple example to familiarize with the writing method of the XSLT style sheet. The converted XML document is shown in Code 1. 1.

Code 1: Hello.xml

Hello!

The top element is "document", which has a sub-element "content". Convert the XML document to the XSLT style indicated by the HTML document, such as code 2.

Code 2: Hello.xsl

The XSLT style sheet is also an XML document, so the first line writes an XML declaration. The second behavior of the XSLT style sheet. The namespace is specified here. In the XSLT style sheet, the XSLT element and the element as the output result are mixed together, so the URI of the XSLT's namespace must be distinguished by namespaces "http://www.w3.org/1999/xsl/ "" "" "" "

The XSL: Template element defines how the processing of each node is called a template. Two templates are defined in Table 2.

This tag marks the beginning of the template for the Document element. The specified string specified in the Match property is the element contained in the converted XML document. Templates for Document elements are first output HTML tag and . The subsequent XSL: Apply-Templates elements indicate the template corresponding to the child elements of the Document element.

Then output and tags. Finally, the end tag of XSL: Template.

This will define a template rule. Since the template to be applied to child elements is indicated in a template for the Document element, you need to define a template for sub-elements. The sub-elements of Document are Content, so the template is defined for the tag.

The HTML tag

is first output to the template of the Content element, then output the contents of the Content element.

XSL: value-of element outputs the node specified by the SELECT property as a string. "." It can be considered to represent the elements itself (here the template to define the Content, so it can be considered a Content element). Finally output tag, end.

The results of the XSLT analyzer analysis are shown in Code 3. 2.

Code 3: Hello.html

Hello!

3 conclusion

We introduced a very simple XSLT style sheet, you should grasp the outline. Create a XSLT style sheet, where the center is the definition template. Even if you only create a simple template and combine them, you can make very delicate conversions. However, the elements of XSLT are not just these. Chapter 2 briefing various elements of XSLT.

Note 1 The text coding that is not particularly indicated herein is UTF-8. For the UTF-8 editor, see the file column1.txt. In addition, in order to make the code more readable, it is possible to change the wrap and indentation of the code. 2 The content of this article uses Xalan as an XSL processor to confirm. For information on using Xalan, see file column2.txt. In addition, this paper does not given the output results of the XSLT directly, but to increase readability, adjustment of the wrap and indentation.

转载请注明原文地址:https://www.9cbs.com/read-112562.html

New Post(0)