XML display -XSL style sheet (on)

zhaozj2021-02-11  222

XML display -XSL style sheet (on)

Beijing University of Posts and Telecommunications

Application and development

A major feature of the XML document is to convert to a variety of format documents. For example, from an XML conversion from a logical structure to another logical structure, or converted to a browse HTML document. This XML document is completed by the Extensible Style Language (XSL).

XML document conversion principle

The important idea of ​​data format conversion is to treat XML documents as a tree structure, and the process of conversion is the process of generating results trees from the source tree. The XSL style defines the conversion rules of the corresponding part in the source tree and the results tree, and each rule contains a template and corresponds to a mode. The template defines the result of the conversion, and the mode specifies the elements or attribute objects that need to be converted.

The syntax format of the XSL in XML is as follows:

If multiple XSL style orders are referenced in the declaration section, only the first style will take effect, and the rest will be ignored.

XSL's format conversion feature is useful in complex e-commerce solutions. For example, companies and B companies are manufacturers who produce machine parts, including product serial numbers and quality grade rating in the cargo list. A company's file format is as follows:

12980-235

200

And the B company's file format is different, and the relevant information appears in the properties of the elements:

In this way, although the two companies' products are exactly the same, they have set obstacles to the trade between the document formats. The use of XSL style can easily convert a company's document into b, and vice versa.

XML document conversion steps

The conversion process of the XML document is divided into two steps:

● First, construct the source tree according to the XML document, and then convert the source tree to the results tree according to the XSL rule. At present, this conversion protocol has become increasing and independent from XSL, which has become a formally recommended standard for W3C, called XSLT (XSL Transformations);

● After generating the results tree, it can be explained to create a format suitable for display, printing, or playback, which is a formatting.

The XSL processor is responsible for implementing the conversion process. First, the XML document is parsed into a DOM tree stored in memory, then analyzing the document, each of the nodes in each DOM tree compares, when the two matches, will be converted according to the rules defined in the template Otherwise, continue to match. This is cycled until the entire document is processed.

XSL document standard format

The standard format of the XSL document is as follows:

Template rule i

Output Template

The XSL document itself is a good format XML document, so pays attention to the matching problem of the label when writing. is both a declaration statement of XSL and a root element, and must be in the head of the file. The namespace of XSL is usually used to specify the namespace of XSL using the XMLNs attribute. All template rules in the style sheet are indicated by the label . Template rules can explain the processing object (element / attribute), processing method or result of conversion. At this point, we can simply understand the label as the concept of functions in the programming language. XSL grammatical structure

The XSL logical syntax structure includes cyclic and conditional judgment. These two structures enable users to flexibly write conversion rules. The loop judgment is implemented via element, its optional attribute includes SELECT and ORDER-BY. The loop structure can be traversed throughout the result, without having to write a conversion rule separately for each result. Its standard syntax format is:

......

Conditional judgment structure is divided into both IF statements and CASE statements. The IF statement is simply judging the condition. The result is true to perform the rules inside the condition, so IF conditions can be used in combination with simple Boolean expressions. The following example is the "Overpaid Employee" information for staff who exceeds more than 1 million yuan:

Overpaid Employee

The CASE statement is a branch of a variety of situations. This statement includes , and three elements. The following example is to output "NO TAX" to the staff of less than 10,000 yuan, and output "High Tax Rate" for staff of more than 50,000 yuan, and output "Normal Tax Rate" information on the staff between them:

NO TAX

HIGH TAX RATE

Normal Tax Rate

XSL template rules

The text content within the tag describes the form of conversion results, called an output template. The value of the property Match compares the template rule with the specified element or attribute, only the matching DOM node will be processed, and the remaining nodes will be ignored. The top of the tree is the root node of the tree, and the root node is represented by "/":

Output Template for root element

Then match other nodes, at this time, as long as you indicate the name of the element object to be processed in quotation marks. If "*" appears in quotation marks, then the rule applies to all element nodes that are not specified separately. For example, the second template in the next example means that all nodes to be processed except for the element:

Output Template

Output Template

In addition, the path indicator can also be used in XSL to specify elements of some special locations to match the template. "//" represents any depth position, such as Used to match any location of any location in the document; if it is , Then it is to match all elements in the subsequent node of the element. Another path indicator is "/", indicating a direct parent-child node relationship. The "//" in the example is "//" means that the element in the element sub-node is matched.

Obviously, some tree nodes may correspond to multiple templates in XSL. In this case, only the last corresponding template takes effect, the front template rules are ignored by the XSL processor.

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

New Post(0)