Example of a code _xml parsing XML into XHTML in XSLT

Source: Internet
Author: User
Tags cdata xml stylesheet xpath xsl xsl file xslt
The basic format for parsing using XSLT is this:
Copy Code code as follows:

<?xml version= "1.0" encoding= "GB2312"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= ' html ' version= ' 1.0 ' encoding= ' GB2312 ' yes ' indent=
<xsl:template match= "/" >
<body>
This can contain some XHTML tags
</body>
</xsl:template>
</xsl:stylesheet>

XSL refers to the Extended Stylesheet Language (extensible Stylesheet Language), the XSL is an XML stylesheet, and the XSL includes 3 parts: XSLT (a language used to convert XML documents), X-path (one for XML The language of navigation in the document), XSL-FO (a language used to format XML documents), you can find tutorials on the content in the http://www.w3cschool.cn/Web site.
As the previous code describes, XSLT starts with the version of XML and transforms in <xsl:style-sheeet ...>...</xsl:style-sheeet> format.
Because it is the first contact with XSLT, it is not very understanding of it, the following is a list of some of the points I encountered when learning and using it;
first, recursive and pass parameter method:
As an example of a family tree, the XML file is like this:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<?xml-stylesheet type= "text/xsl" href= "digui.xsl"?>
<person name= "Otto" sex= "Mael" age= ">"
<person name= "Sandra" sex= "Mael" age= ">"
<person name= "Lichao" sex= "Femael" age= ">"
<person name= "Zhangsan" sex= "Mael" age= "/>"
</person>
<person name= "Eric" sex= "Femael" age= ">"
<person name= "HaLi" sex= "Mael" age= "/>"
</person>
<person name= "Lisi" sex= "Mael" age= ">"
<person name= "HeLi" sex= "Mael" age= "6"/>
<person name= "Andy" sex= "Femael" age= "/>"
</person>
</person>
</person>

Now to write an XSLT to express the relationship of the family, in fact, the family relationship is a family tree, so we through the output of different levels of contraction to express such a relationship, the largest ancestors, and then according to the generation of their respective indentation, the smallest generation of the last side, indented the most, so that the level of a tree out of the , and the following is the code for the XSL file:
Copy Code code as follows:

<?xml version= "1.0" encoding= "GB2312"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= ' html ' version= ' 1.0 ' encoding= ' GB2312 ' yes ' indent=
<xsl:template match= "/" >
<body>
<xsl:apply-templates select= "Person" >
<xsl:with-param name= "level" select= "' 0"/>
</xsl:apply-templates>
</body>
</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 parameter called level in the template, with <xsl:param name= "level"/> Such syntax, and then in the main template (<xsl:template match= "/" ...). </xsl:template>) Assign a value to a parameter when adding a template,
<xsl:apply-templates select= "Person" > <xsl:with-param name= "Level" select= "' 0 '"/> </xsl: Apply-templates&gt, the value of the parameter level is first assigned "0", and we use the value of the parameter level ($level) to indent the value, such as text-indent:{$level}em, so when rendering XML content, The first layer is not indented, after the first layer is rendered, we pass
<xsl:apply-templates select= "Person" >
<xsl:with-param name= "Level" select= "$level + 2"/>
</xsl:apply-templates>
This method sums up the values of the parameter level to achieve recursion, so each rendering layer, the parameter value plus 2, so that the different levels of indentation to achieve the structure of the family tree; here's another thing to say, we take the values of the node properties by @+ properties like @name.
second, the use of parameters to achieve interlaced color:
Or refer to the example above, and the parameter level, we give the parameter +1 (odd number) instead of +2 (even) when recursion, by (...). test= "$level mod 2 = 0") or (.. test= "$level mod 2 = 1") can be implemented to select odd rows or even rows, we use <xsl:choose> <xsl:when test= "$level mod 2 = 0" >...</xsl:when& Gt <xsl:otherwise>...</xsl:otherwise> </xsl:choose> to the odd rows and even rows of different background-color, so as to achieve the effect of interlaced color, The specific code is as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "GB2312"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= ' html ' version= ' 1.0 ' encoding= ' GB2312 ' yes ' indent=
<xsl:template match= "/" >
<body>
<xsl:apply-templates select= "Person" >
<xsl:with-param name= "level" select= "' 0"/>
</xsl:apply-templates>
</body>
</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>

three, symbol escape, and CDATA syntax:
At present only "<" and ">" to escape into < and > if the expression appears in the two symbols, the page will be in the preview of the error;
In addition, we need to the original file content of the output, including the new line or above and less than the page elements, and so on, this need CDATA This tag, CDATA Full name character data, translated into character data, the data does not escape direct output. The syntax format is as follows:
<! [Cdata[Place the character to be displayed here]]>
For example:
<! [Cdata[<person name= "Ason" ></person>]]>
The content displayed on the page will be "<person name=" Ason "></person>";
The following nouns are not mentioned in several tutorials:
1, Local-name (); Example: <xsl:value-of select= "Local-name ()"/&GT;, this means the name of the current node.
2, <xsl:call-template name= "" mode= "" >...</xsl:call-template>,call-template and Apply-template differences, Simply say apply is an application, call is called.
When applied, the engine automatically searches for the template that matches the matching node of the current select-Specified XPath (the template must have a match of attributes) and uses that template for processing, at which point a select path is required.
Call with other languages called the function, you must specify the Name property, the corresponding, the template must have a Name property, of course, it can also be with-param (of course, the corresponding template in a corresponding param, but this is not mandatory). By setting the Mode property, you can invoke different representations of the same content, defined by yourself.
3, COUNT (ancestor::*) This method is to calculate the number of ancestors of the current node, of course ancestor can also use child,following-sibling and other XPath relations.
In order to meet the multiple conditions of the judgment, XSLT can not be very good support:
Take the family tree as an example, I'm going to take a second-level person named Lichao, if literally we can write <xsl:when test= "count (ancestor::*) =2 && *[@name] = ' Lichao '" >...</xsl:when&gt, but this is written in the preview of the XML will be an error, I also want to use the conditions inside the set of conditions such as the wording, also does not conform to the grammar, check a lot of relevant information, did not find a good solution.
These are some of the thoughts and records of the process of learning and using XSLT to parse XML, and the XSLT is powerful, there are many features that are not involved, and of course I continue to study and study.

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.