Through the introduction of the previous two chapters, we have some understanding of the basic concepts of XSLT and its conversion process. Let's learn the specific grammar of XSLT together. Speaking of syntax is always boring, you can browse it substantially, waiting for you to try to study them when you really need to use XSLT. 3.XSLT Element 3.1 XSL: Template and XSL: Apply-Templates 3.2 XSL: Value-of 3.3 XSL: For-Each 3.4 XSL: IF 3.5 XXSL: Choose, When, OtherWise 3.6 XSL: Sort
3.1 XSL: Template and XSL: Apply-Templates
Template is one of the most important concepts in XSLT. The XSLT file consists of a template, and any XSLT file contains at least one template. The concept of template is like a wooden; if you are a programmer, you can also treat a template as a method, a class, or a module. They can be assembled, or they can be made separately, different templates control different output formats. Template consists of two parts: match pattern and execution. Simple lecture mode Defines which node in the XML source document will be processed by template, and execute what format is defined. Two parts of the corresponding syntax for XSL: Template and XSL: Apply-Templates.
XSL: The syntax of Template is:
XSL: The role of Template is to define a new template. In the property, Name, Priority, and Mode are used to distinguish different templates that match the same node. They are not common attributes. Match property controls the matching mode of the template (Pattern), the matching mode is used to locate which node is handled by the template in the XML source document. A template matches a node. We use an example to help understand: Suppose we have to handle a document containing chapters and paragraphs. We define paragraphs with the PARA element and define chapters with Chapter Elements. Let's take a look at the match attribute can be ignorant. O. 旅 娴 ㄋ 0 迤ヅ 迤ヅ 渌 膒
The following statement means template matches all Para elements and all Chapter elements:
The following statement is written in the template matching root node:
Let's see the Apply-Templates syntax:
XSL: Apply-Templates is used to execute that node is handled by template. You can understand it as a subsidy in the program. SELECT property is used to define the exact node name. XSL: Apply-Templates is always included in the XSL: Template element, like this:
This code means that the touchpad matches the Para node, and the child elements under all PARA will be processed. 3.2 XSL: Value-of XSL: Value-of is used to write text values in the source document to the output document. For example: there is a personal data XML document: XML Version = "1.0" encoding = "ISO-8859-1"> If I want to display the value of the Name element in the XML source document on the output document, I can write XSLT code this: After execution, you will see "Ajie" is displayed separately. Where match = "person" defines the touchpad matching the Person node, XSL: value-of syntax description requires the value of a node, and select = "name" defines the elements that need to be output as Name. Is it true that this process is not a person's name in the database? Of course, XSL: value-of query has more, more complex syntax, because it is related to the function of looking and positioning, we will put it in the following XPath syntax in a careful explanation. The same functions have XSL: Copy-of, the same, the same, it is not repeated. 3.3 XSL: For-Each XSL: for-Each syntax allows you to loop processing selected nodes. For example: there is an XML document containing multiple individual information: XML Version = "1.0" encoding = "ISO-8859-1"> I need to display the name of the owner, you can write the XSLT code: XSL: if the IF condition statement similar to a normal program language allows the setting node to be modified when a condition is met. XSL: The syntax format of IF is: E.g: This code means to detect all elements under the PEOPLE node. If you find a 3.5 XSL: Choose, XSL: WHEN and XSL: Otherwise XSL: if syntax does not have an attribute of ELSE. If we want to do multiple options, then use XSL: Choose / XSL: WHEN / XSL: OtherWise Series Process Control Syntax. For details, please see the following XSL file example: Description: First look for the 3.6 xsl: sort The elements of the XML source document can be reordered in the XSLT, and the syntax sorted is XSL: sort. Example: The following code is to place the document element by NAME.