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:
Xml-stylesheet type = "text / xsl" href = "Employees.xsl"?>
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:
OrderItem>
Order>
And the B company's file format is different, and the relevant information appears in the properties of the elements:
Order>
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
xsl: stylesheet>
The XSL document itself is a good format XML document, so pays attention to the matching problem of the label when writing.
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
......
xsl: for-energy>
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
xsl: if>
The CASE statement is a branch of a variety of situations. This statement includes
NO TAX XSL: WHEN>
HIGH TAX RATE XSL: WHEN>
xsl: choose>
XSL template rules
Output Template for root element
xsl: template>
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
Output Template
xsl: template>
Output Template
xsl: 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
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.