XSLT is easy to get started with the third chapter: Element of XSLT

xiaoxiao2021-03-06  95

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 Description Touchboard matches the entire document (root node), process the root node when performing All PARA elements.

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:

ajie 28

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:

ajie 28 TOM 24 Miable 30 >

I need to display the name of the owner, you can write the XSLT code:

3.4 XSL: IF

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:

Template body

E.g:

This code means to detect all elements under the PEOPLE node. If you find a element, the value of the element is output. Where @ 号 is a consecutive, representing all the elements under the node.

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:

No name available

Description: First look for the attribute value of the attribute value under the PEOPLE node, if you find it, output Ajie with bold; if you do not find the value of the element of Ajie, all elements value Use itfore they are output; if no element is found, "no name available" is displayed.

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.

above The primary grammar of the element of the XSLT, there are many other grammar, such as Import, include, Element, Attribute, Number, Param, etc., which are not explained here. Our goal is to let you have basic concepts of XSLT's syntax, understand XSLT as a powerful function of conversion languages.

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

New Post(0)