Use xslt to parse xml into xhtml

Source: Internet
Author: User
Tags xsl file


The basic format for parsing using xslt 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:

<? 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:

<? 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:

<? 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>

Related Article

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.