With the development of the Internet, more and more information into the Internet, information exchange, retrieval, storage and reuse, such as the urgent need for HTML, the most commonly used markup language has become increasingly stretched. HTML integrates data content with performance, modifiable, poor data retrieval, and XML borrowed from the advantages of HTML and database, program language, the content and performance apart, not only make the search more convenient, more important is the exchange of data between users more convenient, reusable more strong.
XML is a Meta markup language that offers more flexibility for web developers without a number of fixed tags. When we use HTML, the markup simply represents the display of the content, without any association with the content, which is extremely inconvenient for further processing of the document. For example, to represent a personal resume, use the HTML representation as follows:
<HTML>
<BODY>
<table border=1 cellspacing=0>
<TH> name <TD> Euchierne <TH> sex <TD> man <TH> birthday <td>1977.5
<TR>
<TH> Skills <TD colspan=5> Database design and maintenance, web development
</TABLE>
</BODY>
</HTML>
Name Euchierne sex Boy Day 1977.5 skills Database design and maintenance, web development
Here, we can not learn from the mark Th, TD what its content, if the use of XML, the corresponding document (filename: personal resume, XML) can be written in the following form:
<?xml version= "1.0" encoding= "GB2312"?>
<resume>
<name> Euchierne </name>
<sex> male </sex>
<birthday>1977.5</birthday>
<skill> database design and maintenance, Web development </skill>
</resume>
What the previous example looks like in a browser (IE5.0 or later)
Description
version── stipulates the version of the XML document, which can only be 1.0;
encoding── here, the encoding type of the XML document is specified, where the value is "GB2312", which is "Simplified Chinese".
Comparing two examples, using XML we can do custom tags, with tags to indicate the meaning of the content. This provides great convenience for using computers to process documents when exchanging data on the Internet, and we don't get confused by a lot of formatting when we read the source files.
However, since XML does not show the way for tagging, if we look at the above two documents in the viewer (we recommend using IE5.0 or newer versions), we will see that the XML document is not displayed in the form of a table. Can't we just display the document like HTML? The answer is in the negative. Take your resume as an example, you need to create a separate format file that describes how each tag is displayed, as follows (assuming the file name is Resume.css):
resume{Display:block;}
name{display:block; font-size:120%;}
sex{display:block; Text-indent:2em}
birthday{display:block; Text-indent:2em}
skill{display:block; Text-indent:2em}
Description
All of the above are CSS style, we recommend that readers refer to the relevant information familiar with CSS, in the future learning must be used, here due to the length of the relationship is not introduced. After creating the file Resume.css, add the following text after the first line of the resume. xml file:
<?xml:stylesheet type= "Text/css" href= "Resume.css"?>
Description
An external CSS style file is referenced here, where type stipulates the style type (preferably text/css or text/xsl), and the href prescribes the file path.
Save the file, and then open the file in IE5.0. What do you think? The format is somewhat different. As if not satisfactory, the document content is clear, but the display effect than the HTML written document is much worse, the XML written documents can only be shown in this way?!
Tips:
1. For better understanding and mastery of XML, it is recommended that you familiarize yourself with HTML 4.0 and CSS 2.0 syntax; master JavaScript, at least one in VBScript; programming experience, knowledge of database theory and SQL can benefit everyone in learning XML.
2. The tags in the XML document must appear in pairs, if the empty tag must have a tag with the same name as "/", or use this type <xml_mark/> to represent the empty tag.
3. XML and the XSL document to be described below, attribute values must be enclosed in double quotes (") or single quotes (').
4. XML documents must be well-formed (XSL documents are also one of the XML documents), that is, tags must have closing tags, tags can be nested but not intersect, such as:
<outer><inner></inner><inner/></outer>
is legal, and the following form
<outer><inner></outer></inner>
Is wrong. If an XML document fails while browsing, it is mostly a violation of the rules mentioned above. [Page]
In the last period we talked about formatting XML documents with CSS (cascading style sheets), and the effect was not very satisfying. In fact, CSS is a good place to format HTML tags, just because it's simple to use in the example above.
XML is just a data file in more time, how can it be changed into the HTML format that we see everyday? If we compare an XML file to a structured raw material, then the XSL is like "sieve" and "die", and the sieve chooses the raw material that it needs, which then forms the final product through the mold: HTML.
The model is roughly the same: we first design the page of performance, then "Dig out" the part of the content that needs to be fetched from the XML, and then use the XSL statement to sift out the relevant data from the XML to populate it. A word to alarm: This XSL is actually a "shell" of HTML, which uses the "shells" to generate "traditional" HTML.
XML in the expansion is a tree structure, we will be in the tree structure of custom tags called nodes, there is a parent-child relationship between the nodes, we want to access the node from the root node to the "/" to the layer of entry.
In the XSL shell, we need to extract the relevant data from the raw Material library XML and use the modal query language provided by the XSL. The so-called modal query language is the specific statement that extracts data from XML through relevant pattern matching rule expressions, that is, the "sieve" mentioned above.
Referring to Microsoft's "XSL Developer's Guide", we can roughly divide the pattern language into three categories:
Select mode:
<xsl:for-each>, <xsl:value-of> and <xsl:apply-templates>
Test mode:
<xsl:if> and <xsl:when>
Match mode:
<xsl:template>
We are now going to introduce them separately.
First, the choice mode
The select-mode statement extracts data from XML as a simple way to get data, which has a select attribute that selects data from specific node names in the XML.
1, <xsl:for-each>
If you have such data in XML:
<author>
<name> Xiao Yu </name>
<name> Chun </name>
<name> Qiushi </name>
</author>
Are we going to read the names of these three authors, one by one, by the "Author/name" method, and can we have more than one name? If there is a procedural statement to iterate read how good Ah!
That's right, XSL provides such a program-language-like statement:<xsl:for-each>
The method used to read the names of these three authors is as follows:
<xsl:for-each select= "Author/name" >
......
</xsl:for-each>
Select, as the name suggests, selects, it can select a specific unique tag in the XML, or you can choose the same kind of tag, which we call the set of nodes.
Grammar:
<xsl:for-each select= "pattern" order-by= "sort-criteria-list" >
Property:
1.select
Use this style description to determine which type of node set (satisfies the select condition) based on the XSL style query context. As a simplified representation, you can make a select equal to the tag name of this element if you want to format the display of the content of one of the tags in the document. For example, to format a tag Xml_mark, you can represent it in the following way:
<xsl:for-each select= "Xml_mark" >
<!--style definition-->
</xsl:for-each>
2.order-by
a semicolon (;)-delimited list of sorting criteria. Adding a plus sign (+) before a list element means that the contents of this tag are sorted in ascending order, and a minus sign (-) is used to indicate the order of reverse orders. As a simplified representation, the sorting criteria list is a sequence of tokens that are specified by select, separated by (;) between each tag.
2, <xsl:value-of>
<xsl:for-each> mode just select the node, and did not take out the value of the node, like monkeys just climbed to a certain branch of the tree, then use < xsl:value-of > to pick the "fruits of victory" bar!
Grammar:
<xsl:value-of select= "pattern" > Extract the value of a node
Property:
The XSL style that the select uses to match the current context. To put it simply, if you want to insert the contents of an XML tag (assumed to be a xml_mark tag) somewhere in an XSL document, you can use the following notation:
<xsl:value-of select= "Xml_mark" ></xsl:value-of>
Or
<xsl:value-of select= "Xml_mark"/>
Example:
As an example of the above resume, we need to make some changes to the document (CV. xml), which is to say the second line:
<?xml:stylesheet type= "Text/css" href= "Resume.css"?>
Modified to:
<?xml:stylesheet type= "text/xsl" href= "resume.xsl"?>
Then create a new file: Resume.xsl, which reads as follows:
<?xml version= "1.0" encoding= "GB2312"?>
<HEAD>
<TITLE> Personal Resume </TITLE>
</HEAD><BODY>
<xsl:for-each select= "Resume" >
<P/>
<table border= "1" cellspacing= "0" >
<caption style= "font-size:150%; Font-weight:bold ">
Personal Resume
</CAPTION>
<TR>
<TH> name </th><td><xsl:value-of select= "name"/></td>
<TH> sex </th><td><xsl:value-of select= "Sex"/></td>
<TH> birthday </th><td><xsl:value-of select= "Birthday"/></td>
</TR>
<TR>
<TH> Skills </th><td colspan= "5" ><xsl:value-of select= "skill"/></td>
</TR>
</TABLE>
</xsl:for-each>
</BODY>
</HTML>
Let's take a look at the results of our hard work after we finish these. It worked out fine. The cooler is still behind us. Now we make further changes to the document (CV. xml):
1. Add a new tag before <resume> Mark <document>;
2. Copy and paste the contents (including this pair of tags) between the mark and the <resume></resume>, and then end with <document> at the end.
3. Open file resume.xsl with Notepad.exe, add text after Mark <HTML>: <xsl:for-each select= "Document" >; in Mark </HTML> Add text before: </xsl:for-each>, save file.
4. Open the file in the browser (CV. xml). What did you see? Two personal resumes!
In this way, using XML we can write content and style to complete the separation of documents! Of course, XSL files are more complex than normal HTML files, but once done, they can be used to format all of the same XML documents. [Page]
After a few days of learning, we learned about the authoring of XHTML documents and three of XSL elements that have been able to write fairly flexible XSL documents, and today we will learn about the writing of XSL templates. As we all know, short documents and programs are very readable, but when scaled up, their complexity increases at a faster rate.
We have learned <xsl:for-each>, <xsl:value-of>, and so on, we can use them to achieve simple format output of XML data, but if you encounter more complex XML format output, the XSL in order to write down as required, First, the design is difficult, scalability is poor, not conducive to the division of labor between the cooperation between the other, can be modified very poor, may appear to lead the whole army of the situation, not conducive to maintenance. The process of modular design step-by-step refinement of the method here has been applied!
The XSL template refines the design of the XSL into a template (block) Finally, the templates (blocks) are combined into a complete XSL; like ships and containers, we don't pile up all the goods in pieces, we load them in separate containers and then pile them up on the ship. This method allows you to consider the overall design of the entire XSL, and then a number of forms into different modules, and then specifically design these modules, and finally integrate them together, so that the macro and micro, in line with the people of the Organization, standardization requirements.
Loading container--writing template (block):<xsl:template>
Grammar:
<xsl:template match= "Node-context" language= "Language-name" >
Property:
match── determine what circumstances to execute this template. As a simplified description, use the name of the tag here, where the top-level template must set the match to "/".
language── determines what scripting language is executed in this template with the same value as the Language property of the script tag in HTML, and the default value is JScript.
<xsl:template> use the match attribute to select a node from XML that satisfies the condition, and a template that forms a specific output form for these specific nodes.
Hanging container on board--calling template (block):<xsl:apply-templates>
Grammar:
<xsl:apply-templates select= "pattern" order-by= "sort-criteria-list" >
Property:
select── determines what template should be executed in this context, that is, select the template (block) that was established with the < Xsl:template > tag.
order-by── a sort standard separated by semicolons (;), usually a sequence of child tags.
Example:
Take a personal resume as an example, in order to facilitate the process we would like to "skills" each item is tagged to <skill></skill> around, how many skills there are many of these tags on, after the revised resume XML document contents are as follows:
<?xml version= "1.0" encoding= "GB2312"?>
<?xml:stylesheet type= "text/xsl" href= "resume_template.xsl"?>
<document>
<resume>
<name> Euchierne </name>
<sex> male </sex>
<birthday>1977.5</birthday>
<skill> database design and Maintenance </skill>
<skill>web Development </skill>
</resume>
</document>
Then, create a new XSL file, resume_template.xsl, in the form of a template, which reads as follows:
<?xml version= "1.0" encoding= "GB2312"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<!--root template-->
<xsl:template match= "/" >
<HTML><HEAD><TITLE> Personal Resume </TITLE></HEAD>
<BODY>
<xsl:apply-templates select= "Document/resume"/>
</BODY>
</HTML>
</xsl:template>
<!--resume Template-->
<xsl:template match= "Resume" >
<table border= "1" cellspacing= "0" >
<CAPTION> Personal Resume (
<xsl:eval>formatindex (Childnumber (this), "I") </xsl:eval>
) </CAPTION>
<xsl:apply-templates select= "Name"/>
<xsl:apply-templates select= "Sex"/>
<xsl:apply-templates select= "Birthday"/>
<TR/>
<TD> Skills </td><td colspan= "5" >
<table cellspacing= "0" >
<xsl:apply-templates select= "Skill"/>
</TABLE>
</TD>
</TABLE>
<BR/>
</xsl:template>
<!--name Template-->
<xsl:template match= "name" ><TD> name </TD>
<TD><xsl:value-of/></TD>
</xsl:template>
<!--gender template-->
<xsl:template match= "Sex" ><TD> sex </TD>
<TD><xsl:value-of/></TD>
</xsl:template>
<!--birthday template-->
<xsl:template match= "Birthday" ><TD> birthday </TD>
<TD><xsl:value-of/></TD>
</xsl:template>
<!--skills Template-->
<xsl:template match= "Skill" >
<TR><TD><xsl:value-of/></TD></TR>
</xsl:template>
</xsl:stylesheet>
Save the file, open the file (resume. xml), the effect is satisfactory. In fact, to do the same effect, with the previous three weeks to introduce the method can be done, but you have to consider it as a whole.
In the XSL file above, we write the data items, such as sex, birthdays, skills and so on separately, and then use the <xsl:apply-template> to call them, so that even if you want to modify and expand the templates in the future, it will be convenient to avoid interference, Mixed up with confusion. The design method of thinning from top to bottom and layer by step greatly reduces the complexity of the work and greatly reduces the generation of errors, and can realize the collaborative design of many people.
Note: If different tags in the XML document have a child tag with the same name, the parent tag should be prefixed with the form (Parent_mark/child_mark) when writing the template for it. The template file must have a root template whose properties match is "/". [Page]
One of the advantages of XML technology is the selectivity of data output, that is, to select the required data output. The selection pattern statement:<xsl:for-each>, <xsl:value-of>, and <xsl:apply-template> that we talked about earlier simply select the nodes that arrive through the "/" symbol layer. If we do not need all of the XML data output, and only need to meet a certain condition of some of the data, "radish greens, to do everything", then conditional judgment <xsl:if> and multiple conditions of judgment <xsl:choose> and <xsl:when> To cater to this need, if you are familiar with the design, you will find them familiar.
If in XSL, first, introduce the syntax structure of the XSL element <xsl:if>:
Grammar:
<xsl:if expr= "script-expression" language= "language-name" test= "pattern" >
Property:
expr── a script language expression that evaluates to True or false, and if the result is true, and the content is displayed in the output (this property can be omitted).
The script language type of an expression in the Language──expr property, with the same value as the Language property of the HTML tag script, and the default is JScript.
test── source data test conditions.
Example:
This is in the case of a report named Report.xml, which reads as follows:
<?xml version= "1.0" encoding= "GB2312"?>
<?xml:stylesheet type= "text/xsl" href= "report.xsl"?>
<document>
<report>
<class>
Jia ban
</class>
<q1>50</q1>
<q2>70</q2>
<q3>30</q3>
<q4>10</q4>
</report>
<report>
<class>
Class B
</class>
<q1>20</q1>
<q2>30</q2>
<q3>40</q3>
<q4>50</q4>
</report>
<report>
<class>
Class C
</class>
<q1>70</q1>
<q2>40</q2>
<q3>20</q3>
<q4>10</q4>
</report>
</document>
We use the XSL template in conjunction with the <xsl:if> we learned today, and write an XSL document for it, requiring a quarterly yield of less than or equal to 20 in red, the file name is report.xsl and the contents are as follows:
<?xml version= "1.0" encoding= "GB2312"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<body><xsl:apply-templates select= "Document"/></body>
</HTML>
</xsl:template>
<xsl:template match= "Document" >
<table border= "1" cellspacing= "0" >
<TH> Team </TH>
<TH> First quarter </TH>
<TH> Two quarterly </TH>
<TH> Three quarterly </TH>
<TH> Four Quarterly </TH>
<xsl:apply-templates select= "The/>"
</TABLE>
</xsl:template>
<xsl:template match= "the" >
<TR>
<td><xsl:value-of select= "Class"/></td>
<td><xsl:apply-templates select= "Q1"/></td>
<td><xsl:apply-templates select= "Q2"/></td>
<td><xsl:apply-templates select= "Q3"/></td>
<td><xsl:apply-templates select= "Q4"/></td>
</TR>
</xsl:template>
<xsl:template match= "Q1|q2|q3|q4" >
<!--here to test output, such as less than or equal to 20, add a style property color with red (red)-->
<xsl:if test= ". [Value () $le $] ">
<xsl:attribute name= "Style" >color:red</xsl:attribute>
</xsl:if>
<xsl:value-of/>
</xsl:template>
</xsl:stylesheet>
Description
q1|q2|q3|q4── tags q1, q2, Q3, Q3 Use this template to determine the output
$le $── is "less than equal" in the relational operator, other relationships are less than ($LT $), greater than ($GT $), greater than or equal to ($ge $), equal to ($eq $), not equal to ($ne $), and so on.
. --refers to the current tag.
[]── represents a filter, only the tags that meet the filter criteria can be selected.
Value () ──xsl function, other common XSL functions have text (), end (), index (), and so on.
In the next phase, we will learn about the other three elements of XSL that can be tested multiple times for the same data, producing the corresponding output according to different conditions. [Page]
In the last period we learned about the XSL element <xsl:if>, we have been able to test the value of XML data to determine the different output form, I do not know whether you have tried, actually <xsl:for-each> can also partially implement <xsl:if> functions, but sometimes , we want to test multiple conditions at the same time, and output corresponding results according to different conditions. Of course, we can use if if we only have if available. Fortunately we have a better choice, that is to use <xsl:choose>. The syntax for the related elements is described below:
<xsl:choose>
Grammar:<xsl:choose>
Properties: None, representing the start of a multiple-selection test
<xsl:when>
Grammar:
<xsl:when expr= "script-expression" language= "language-name" test= "pattern" >
Property:
expr── a script language expression that evaluates to True or false, and if the result is true, and the content is displayed in the output (this property can be omitted).
The script language type of an expression in the Language──expr property, with the same value as the Language property of the HTML tag script, and the default is JScript.
test── source data test conditions.
<xsl:otherwise>
Grammar:<xsl:otherwise>
Properties: None, in a multiple-selection test, if there are no conditions that do not meet the <xsl:when>, if the tag is at the end, the contents of the tag are output.
Example:
In this case, the student report card, for example, requires excellent (>85), General (70~85), passing (60~69), failing (< 60), rather than showing the score according to the level of achievement. One of the transcripts of the XML document (filename: grade.xml) is as follows:
<?xml version= "1.0" encoding= "GB2312"?>
<?xml:stylesheet type= "text/xsl" href= "grade.xsl"?>
<document>
<grade>
<name> Big Fat </name>
<english>80</english>
<math>90</math>
<chymest>90</chymest>
</grade>
<grade>
<name> Floret </name>
<english>98</english>
<math>70</math>
<chymest>85</chymest>
</grade>
</document>
The XSL document (filename: grade.xsl) reads as follows for the implementation to be displayed by fractional rank:
<?xml version= "1.0" encoding= "GB2312"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:template match= "/" >
<HTML>
<HEAD><TITLE> Transcript </TITLE></HEAD>
<BODY>
<xsl:apply-templates select= "Document"/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match= "Document" >
<table border= "1" cellspacing= "0" >
<TH> name </TH><TH> English </TH><TH> math </TH><TH> Chemistry </TH>
<xsl:apply-templates select= "Grade"/>
</TABLE>
</xsl:template>
<xsl:template match= "Grade" >
<TR>
<td><xsl:apply-templates select= "Name"/></td>
<td><xsl:apply-templates select= "中文版"/></td>
<td><xsl:apply-templates select= "Math"/></td>
<td><xsl:apply-templates select= "Chymest"/></td>
</TR>
</xsl:template>
<xsl:template match= "Name" >
<xsl:value-of/>
</xsl:template>
<xsl:template match= "English|math|chymest" >
<xsl:choose>
<xsl:when test= ". [Value () $gt $85] "> Excellent </xsl:when>
<xsl:when test= ". [Value () $gt $70] "> General </xsl:when>
<xsl:when test= ". [Value () $GT $] "> Grid </xsl:when>
<xsl:otherwise> No grid </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Note: In <xsl:choose> selection, start with the first <xsl:when>, test each one, until you meet a test condition, output the contents of it, no longer test the following conditions; If you do not meet any of the criteria, output <xsl:o The contents of the therwise>.
Tags for <xsl:when></xsl:when> and <xsl:otherwise></xsl:otherwise> can be nested <xsl:if> or <xsl: Choose>.