Study xsl with me (iii)

Source: Internet
Author: User
Tags eval script tag version what scripting xsl xsl file

XSL templates and matching patterns

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, can use them to realize simple formatted output of XML data, but if you encounter more complex XML format output, write down the XSL 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 containers?? Writing template (block):< xsl:template >

< 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 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, which is the default value of JScript

< Xsl:template > uses the match attribute to select a node that satisfies the condition from the XML, and a template that forms a specific output form for these specific nodes.



Hanging container on board-?? Call Template (block):< xsl:apply-templates >

< 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 semicolon (;)-delimited sorting criterion, usually a sequence of child tags


Example:

Take a personal resume as an example, in order to facilitate the processing we want "skills" in each item marked to < skill ></skill > surrounded by 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 > Men </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 >< BODY >
< xsl:apply-templates select= "Document/resume"/>
</body >
</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 with the template, and then use the < Xsl:apply-template >, so that even if you want to modify and expand these templates in the future, it is very 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.


Attention:
If a different tag in an XML document has a child tag with the same name, the parent tag should be prefixed with the form (Parent_mark/child_mark) when you write the template for it.
The template file must have a root template whose properties match is "/".



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.