A major feature of the xml| display of XML documents is that they can be converted to documents in multiple formats. For example, from a logical structure of XML to another logical structure of the XML conversion, or to the conversion of the HTML document can be browsed. The data format conversion functionality of this XML document is accomplished by an extensible Style single language (XSL).
Principles of XML Document conversion
The important idea of data format conversion is to treat XML document as a kind of tree structure, and the process of transformation is to generate the result tree from source tree. The XSL style sheet defines the transformation rules for the corresponding parts of the source tree and the result tree, each of which contains a template and corresponds to a pattern. The template defines the result of the transformation, and the schema sets the element or attribute object that needs to be converted.
The syntax format for referencing XSL in XML is as follows:
<?xml-stylesheet type= "text/xsl" href= "employees.xsl"?>
If more than one XSL style sheet is referenced in the Declaration section, only the first style sheet will take effect and the rest will be ignored.
The format conversion capabilities of XSL are useful in complex e-business solutions. For example, company A and b are the manufacturers of machine parts, which include the serial number and quality grade score in the cargo list. A company's file format is as follows:
<Order>
<OrderItem>
<ItemID>12980-235</ItemID>
<Quantity>200</Quantity>
</OrderItem>
</Order>
and B Company's file format is different, the relevant information appears in the attributes of the element:
<Order>
<orderline partno= "12980-235" numrequired= "/>"
</Order>
In this way, although the products of the two companies are identical, the differences in document formats have set up barriers to trade between the parties. Using an XSL style sheet makes it easy to convert a company's documents into a B company format, and vice versa.
Second, the XML document conversion steps
The conversion process for XML documents is divided into two steps:
The source tree is constructed from the XML document, and then the source tree is converted to the result tree according to the XSL rules. At present, this transformation protocol is becoming more and more perfect, and is independent from XSL, become the standard of the official recommendation of the Consortium, called XSLT (XSL transformations);
Once the result tree is generated, it can be interpreted to produce a format suitable for display, printing, or playback, which is called formatting (formatting).
The XSL processor is responsible for implementing the conversion process. First, the XML document is parsed into a DOM tree and stored in memory, then the document is parsed, each node in the DOM tree is compared to a pattern, and when they match, they are converted according to the rules defined in the template, otherwise they continue to match. This loops until the entire document is processed.
Standard format of XSL documents
The standard format for XSL documents is as follows:
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
Template rule I
Output template
</xsl:stylesheet>
The XSL document itself is a well-formed XML document, so you should pay attention to the matching of labels when you write. <xsl:stylesheet> is both an XSL declaration statement and a root element, and must be in the header of the file. It is also common to use the xmlns attribute to indicate the XSL namespace. All the template rules in the style sheet are indicated by the label <xsl:temlplate>. Template rules can describe the objects (elements/attributes) that are processed, how they are handled, or the results of the transformations. At this point, we can look at the label like the concept of a function in a programming language.
Iv. the grammatical structure of XSL
The logical grammatical structure of XSL includes loops and conditional judgments. These two structures give users the flexibility to write the rules of the transformation. The circular judgment is implemented through the <xsl:for-each> element, and its optional attributes include Select and order-by. The loop structure is able to traverse the entire result set without having to write the conversion rules individually for each result. Its standard syntax format is:
<xsl:for-each select= "pattern" order-by= "patternlist" >
......
</xsl:for-each>
The conditional judgment structure is divided into if statement and Case statement two forms. If statement is simply to judge the condition, the result is true to execute the rules within the condition, so you can combine the IF condition with a simple Boolean expression. The following example is the output of "overpaid employee" information for employees who pay more than 1 million yuan:
<xsl:if match= ". [Salary $GT $1000000] ">
Overpaid employee
</xsl:if>
Case statements are the branch judgments of multiple situations. The statement includes <xsl:choose>, <xsl:when>, and <xsl:otherwise> three elements. The following example is the output of "No tax" to employees with a salary of less than 10,000 yuan, the output of "high tax rate" for employees over 50,000 yuan, and the "Normal tax rate" information for staff in between:
<xsl:choose>
<xsl:when match= ". [Salary $LT $10000] ">
No Tax </xsl:when>
<xsl:when match= ". [Salary $GT $50000] ">
High tax rate </xsl:when>
<xsl:otherwise> Normal Tax Rate </xsl:otherwise>
</xsl:choose>
V. Template rules for XSL
The text content within the <xsl:template> label describes the form of the transformation result, called the output template. The value of the attribute match compares the template rule to the specified element or attribute, only the matching DOM nodes are processed, and the remaining nodes are ignored. The root node of the tree is the first match in the process, and the root node is represented by "/":
<xsl:template match= "/" >
Output template for root element
</xsl:template>
Then match the other nodes, at which point the name of the element object to be processed is indicated in quotation marks. If a "*" appears in quotation marks, it means that the rule applies to all element nodes that are not individually specified for processing. For example, the second template in the following example indicates that all nodes except the <Employee> element are to be processed:
<xsl:template match= "Employee" >
Output template
</xsl:template>
<xsl:template match= "*" >
Output template
</xsl:template>
In addition, you can use the path designator in the XSL to specify that elements of a particular location match the template. "//" represents an arbitrary depth position, such as <xsl:template match= "//employee" > The <Employee> element used to match any position in the document; if <xsl:template match= " Employee//name "indicates that all <Name> elements in the successor node of the <Employee> element are matched. Another path indicator is "/", which indicates a direct parent-child node relationship. Changing the "//" to "/" in the example above means that the <Name> element in the <Employee> element child node is matched.
Obviously, some tree nodes may correspond to multiple templates in the XSL, in which case only the last corresponding template will take effect, and the preceding template rule will be ignored by the XSL processor.
I. Use of XSL templates
XSL describes the output format in the output template, which can be a variety of strings, tag symbols, node values, or some XSL syntax structures, such as conditional judgment, loop processing, and so on. In many applications, the output template needs to use the value of the node, at this time can be used to use the <xsl:value-of> element output node value, the most direct use is <xsl:value-of/> This outputs the values of the current node and all subsequent nodes. If you simply want to output the value of the specified node, you can qualify with the Select property (the Select property can be any valid path expression):
<xsl:value-of select = "Name"/>
<xsl:value-of select = "//employee"/>
The first of these expressions matches the element named <Name> in all child nodes of the current node, and the second expression matches the element named <Employee> in all subsequent nodes in the current node. Note: There must be a template rule in the XSL style sheet that matches the root element.
After you have determined that the template rules match the elements, you can activate the template, which is done by the <xsl:apply-templates> element. It combines with the <xsl:templates> element as a function call in programming: The former is the calling instruction, and the latter is the function body. For different elements, you need to invoke different templates for processing. To activate template rules in a style sheet, you use the <xsl:apply-templates> element in the root element template rule, which makes the entire style sheet file effective:
<xsl:template match= "/" >
<xsl:apply-templates/></xsl:template>
Use the <xsl:apply-templates> element directly to apply a template to all child nodes of the current node without discrimination, while writing a match in the Select property can qualify the object:
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<xsl:apply-templates select= "//employee"/>
</xsl:template>
<xsl:template match= "Employee" ><P>
<xsl:apply-templates select= "Name"/>
<xsl:apply-templates select= "Salary"/>
</P></xsl:template>
<xsl:template match= "Name" >
<span style= "font-size:36pt" >
<xsl:value-of/> </SPAN></xsl:template>
<xsl:template match= "Salary" >
<i><xsl:value-of/></i></xsl:template>
</xsl:stylesheet>
The first template above matches the root element of the XML document and applies template rules to all <Employee> elements below the root node. Then, once you encounter the <Employee> tag, insert a <P> tag as a blank paragraph, and then apply the template rules to the <Name> elements and <Salary> elements, respectively. Finally, the result of the conversion display is the 36-pound font of the employee name and the employee's salary in italics.
Second, the XSL extension rules
1. Path indicator
In addition to the "//" and "/" path indicators described earlier, as well as the wildcard character "*", there are several symbols that can be used to restrict the matching objects of the template:
The current node indicator is "." ;
The parent node indicator is ".." ;
The property indicator is "@".
The above "@" indicates a match to a property in the specified element, such as <xsl:apply-templates select= "employee/@ID"/> Statement for <ID> in the <Employee> element property to apply a template rule.
2. Filter matching characters
In addition to path indicators, you can conditionally filter or sort the objects to further adjust the effect. When filtering, it is generally the case that the child element (or attribute) is present (or its value) as a standard:
Child element exists://employee[salary] exists, selects all <Employee> elements that contain <Salary> child elements;
Child element values: that is, Employee[salary > 25000], select all <Employee> elements that contain <Salary> child elements and Salary values greater than 25000;
Attribute exists: employee[@ID] exists, select <Employee> element containing <ID> attribute;
Property value: That is, the employee[@ID = "1234"], select all the <Employee> elements with a property ID value of 1234.
3. Other extended Filtration
Some of the other additional features of the filter, by function: Comparison operators, Boolean operators and collection index.
The writing format and functionality of the comparison operator are as follows:
operator function
$eq $ equals
$ne $ unequal
$lt $ less than
$le $ less than or equal to
$GT $ greater than
$ge $ greater than or equal to
What needs to be explained is that the operators in the table are sensitive to case characters in the comparison character, and if you want to ignore the different meanings of the case, prefix each operator with the letter "I", such as "$ieq".
Boolean operator writing format and function description are as follows:
operator function
$and $ logic and
$or $ logic or
$not $ take non
The filtered results can be filtered through the collection index. For example, Employee[salary][2] is the 2nd to select all <Employee> elements that contain <Salary> child elements. In addition, XSL provides a collection index function for users to use: The index method represents the indexed number of the filtered result, and the End method represents the last filter result. The use method looks like this:
Employee[index () $lt $2]
Employee[end ()]
The selection result of the first expression above is the 1th and 2nd child elements of the <Employee> element, and the result of the second expression is the last child element of the <Employee> element. By default, the order in which template rules match elements is scheduled in the order in which they appear in the XML document. However, in specific applications, you may need to adjust the original order, at which point you need to use the Order-by property. The use method looks like this:
<xsl:apply-templates select= "//employee" order-by= "+name"/>
<xsl:apply-templates select= "//employee"
order-by= "Number (Salary)"/>
The first example above indicates that the alphabetical order of names should be sorted by name, such as "Bob" should precede "Tom", while the second example would indicate how much the employee was sorted according to the salary.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.