Parse XML into XHTML code using XSLT

Source: Internet
Author: User
Tags xsl file xslt

The basic format for parsing using XSLT is as follows: CopyCode The Code is as follows: <? XML version = "1.0" encoding = "gb2312"?>
<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: output method = 'html' version = '1. 0' encoding = 'gb2312 'indent = 'yes'/>
<XSL: template match = "/">
<HTML>
<Body>
// Some XHTML labels can be contained here
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>

XSL refers to the Extensible Stylesheet Language. XSL is an XML style table. XSL consists of three parts: XSLT (a language used to convert XML documents) x-path (a language used for navigation in XML documents) and XSL-FO (a language used to format XML documents) can be found at http://www.w3cschool.cn.
As described in the preceding code, XSLT starts with the XML version and uses <XSL: style-sheeet ......>... </XSL: style-sheeet>.
Because I first came into contact with XSLT, I did not have a thorough understanding of it. The following is just a list of some of the key points I encountered when learning and using it;
1. recursive and parameter passing methods:
Taking a family tree as an example, the XML file is like this: Copy code The Code is as follows: <? XML version = "1.0" encoding = "ISO-8859-1"?>
<? XML-stylesheet type = "text/XSL" href = "digui. XSL"?>
<Person name = "Otto" Sex = "mael" age = "60">
<Person name = "Sandra" Sex = "mael" age = "35">
<Person name = "Lichao" Sex = "femael" age = "34">
<Person name = "zhangsan" Sex = "mael" age = "12"/>
</Person>
<Person name = "Eric" Sex = "femael" age = "36">
<Person name = "Hali" Sex = "mael" age = "18"/>
</Person>
<Person name = "Lisi" Sex = "mael" age = "30">
<Person name = "heli" Sex = "mael" age = "6"/>
<Person name = "Andy" Sex = "femael" age = "13"/>
</Person>
</Person>
</Person>

Now we need to write an XSLT to express the family relationship. In fact, the family relationship is a family tree, so we can express this relationship by outputting indentation at different levels, the largest ancestor, and then Indented by generation respectively. The smallest generation is at the end and the most indented, so that the hierarchy of a tree will come out. The following is the code of the XSL file:Copy code The Code is as follows: <? XML version = "1.0" encoding = "gb2312"?>
<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: output method = 'html' version = '1. 0' encoding = 'gb2312 'indent = 'yes'/>
<XSL: template match = "/">
<HTML>
<Body>
<XSL: Apply-templates select = "person">
<XSL: With-Param name = "level" select = "'0'"/>
</XSL: Apply-templates>
</Body>
</Html>
</XSL: Template>
<XSL: template match = "person">
<XSL: Param name = "level"/>
<P style = "text-indent: {$ level} em"> name: <XSL: value-of select = "@ name"/>, sex: <XSL: value-of select = "@ sex"/>, age: <XSL: value-of select = "@ age"/> </P>
<XSL: Apply-templates select = "person">
<XSL: With-Param name = "level" select = "$ LEVEL + 2"/>
</XSL: Apply-templates>
</XSL: Template>
</XSL: stylesheet>

Here, we first define a level parameter in the template, use the <XSL: Param name = "level"/> syntax, and then in the main template (<XSL: template match = "/">... </XSL: Template>) assign values to parameters when adding a template,
<XSL: Apply-templates select = "person"> <XSL: With-Param name = "level" select = "'0'"/> </XSL: apply-templates>, the value of the parameter level is initially assigned as "0", and we use the value of the parameter level ($ level) as the indent value, such as text-indent: {$ level} em, so when the XML content is rendered, the first layer is not indented. After the first layer is rendered
<XSL: Apply-templates select = "person">
<XSL: With-Param name = "level" select = "$ LEVEL + 2"/>
</XSL: Apply-templates>
In this way, the value of the parameter level is accumulated to implement recursion. In this way, 2 is added to the parameter value for each layer of rendering, in this way, different levels of indentation are implemented to implement the structure of the family tree. Here, we also need to take the value of the node attribute through the @ + attribute, such as @ name.
2. Use parameters to implement line-based color change:
We still reference the above example and the parameter level. In recursion, we give the parameter + 1 (ODD) instead of + 2 (even) through (.. test = "$ level mod 2 = 0") or (.. test = "$ level mod 2 = 1") to select an odd or even row. We use <XSL: Choose> <XSL: when test = "$ level mod 2 = 0">... </XSL: When> <XSL: otherwise>... </XSL: otherwise> </XSL: Choose> the background-color of the odd and even rows is used to change the color of each line. The Code is as follows: Copy code The Code is as follows: <? XML version = "1.0" encoding = "gb2312"?>
<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: output method = 'html' version = '1. 0' encoding = 'gb2312 'indent = 'yes'/>
<XSL: template match = "/">
<HTML>
<Body>
<XSL: Apply-templates select = "person">
<XSL: With-Param name = "level" select = "'0'"/>
</XSL: Apply-templates>
</Body>
</Html>
</XSL: Template>
<XSL: template match = "person">
<XSL: Param name = "level"/>
<XSL: Choose>
<XSL: When test = "$ level mod 2 = 0">
<P style = "text-indent: {$ level} em; Background-color: # DDD"> name: <XSL: value-of select = "@ name"/>, sex: <XSL: value-of select = "@ sex"/>, age: <XSL: value-of select = "@ age"/> </P>
</XSL: When>
<XSL: otherwise>
<P style = "text-indent: {$ level} em; Background-color: # Eee"> name: <XSL: value-of select = "@ name"/>, sex: <XSL: value-of select = "@ sex"/>, age: <XSL: value-of select = "@ age"/> </P>
</XSL: otherwise>
<XSL: Apply-templates select = "person">
<XSL: With-Param name = "level" select = "$ LEVEL + 1"/>
</XSL: Apply-templates>
</XSL: Template>
</XSL: stylesheet>

Iii. symbol escaping and CDATA Syntax:
Currently, only "<" and ">" need to be converted to <and>. If the two symbols appear in the expression, the page will report an error during preview;
In addition, we need to output the content of the original file intact, including line breaks or the above page elements such as greater than and less than signs. At this time, we need the CDATA label, which is the full name of character data, translate into character data and output data without escaping. The syntax format is as follows:
<! [CDATA [place the characters to be displayed here]>
For example:
<! [CDATA [<person name = "ASON"> </person>]>
The content displayed on the page will be "<person name =" ASON "> </person> ";
4. terms not mentioned in several Tutorials:
1. Local-Name (); example: <XSL: value-of select = "Local-Name ()"/>, which indicates the name of the current node.
2. <XSL: Call-Template Name = "" mode = "">... </XSL: Call-template>: The difference between call-template and apply-template is that apply is an application and call is a call.
When applying is used, the engine automatically searches for a template that matches the matching node of the current select specified XPath (this template must have an attribute match) and uses this template for processing, in this case, you must specify the select path.
When calling a function in the same way as calling a function in other languages, you must specify the name attribute. The template must have the name attribute. Of course, you can also use with-param (of course there is a corresponding Param in the corresponding template, but this is not mandatory ). You can call different representations of the same content by setting the mode attribute, which is defined by yourself.
3. Count (ancestor: *) is used to calculate the number of ancestor nodes on the current node. Of course, ancestor can also use XPath relationships such as child and following-sibling.
5. XSLT cannot be well supported for multi-condition judgment:
Take the family tree as an example. I want to take a person on the second layer named Lichao. If we can literally write it as <XSL: When test = "count (ancestor :: *) = 2 & * [@ name] = 'lichao' ">... </XSL: When>, but an error will be reported when I write this statement in the preview XML. I also want to write this statement in the condition, which does not conform to the syntax, I checked a lot of relevant information and did not find a good solution.
The above are some of the feelings and records in the process of learning and using XSLT to parse XML. XSLT is very powerful, and many functions are not involved. Of course, I will continue to learn and study it.

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.