XSLT element syntax

xiaoxiao2021-03-06  76

3.xslt elements

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:

match = pattern

Name = qname

Priority = NUMBER

Mode = QNAME>

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 possible value of the Match property. The following statement write description template matches all Para elements

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:

SELECT = Node Set-Expression

Mode = QNAME>

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 Touch Patches Match the entire document (root node), process all PARA elements under the root node when executed.

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. E.g:

There is a personal information 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 the 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.

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

New Post(0)