The style sheet file that can be extended by XSL. You can format the display of xml, or convert xml into another format.
To learn XSL, you must be familiar with XPath. XSL is as simple and powerful as XPath and is easy to learn.
1. Since XSL can format the xml display style, Let's first look at how to reference the xsl file in xml
Example code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<? Xml-stylesheet type = "text/xsl" href = "url. xsl"?>
You only need to add <? Xml-stylesheet type = "text/xsl" href = "url. xsl"?> You can.
2. XSL format
XSL is also a standard xml file. It starts with an xml file declaration. The root element must be xsl: styleshee, and the root element must have the version attribute to specify the xsl version, and xmlns: xsl = "http://www.w3.org/5o/#/transform, as shown in the following example
<? Xml version = "1.0" encoding = "encoding"?>
<Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
3. The main points of Xsl are as follows: xml
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<? Xml-stylesheet type = "text/xsl" href = "pets-templates.xsl"?>
<Pets>
<Pig color = "blue" weight = "100">
<Price> 100 </price>
<Desc> this is a blue pig </desc>
</Pig>
<Cat color = "red" weight = "9">
<Price> 80 </price>
<Desc> this is a red cat </desc>
</Cat>
<Dog color = "green" weight = "15">
<Price> 80 </price>
<Desc> this is a green dog </desc>
</Dog>
<Cat color = "green" weight = "15">
<Price> 80 </price>
<Desc> this is a green cat </desc>
</Cat>
<Dog color = "blue" weight = "10">
<Price> 100 </price>
<Desc> this is a blue dog </desc>
</Dog>
<Dog color = "red" weight = "9">
<Price> 80 </price>
<Desc> this is a red dog </desc>
</Dog>
</Pets>
The preceding xml format is as follows:
1) xsl: template defines the conversion template for matching nodes. The attribute match = "xpath expression" is used to define elements matching the template.
The template that matches the root node is defined as follows:
Copy codeThe Code is as follows: <xsl: template match = "/">
</Xsl: template>
2) xsl: for-each loop display select = "xpath expression" select node conversion (similar to the foreach statement in programming languages ),
In the following example, child elements under pets are selected and the names of child elements are displayed cyclically:Copy codeThe Code is as follows: <xsl: for-each select = "/pets/*">
<Xsl: value-of select = "name ()"/>
</Xsl: for-each>
3) xsl: if element condition display nodes (similar to the if statement in programming languages) should be replaced by <and>
Copy codeThe Code is as follows: <xsl: if test = "@ weight <10">
<I> its weight is less than 10 km </I>
</Xsl: if>
4) xsl: multi-branch condition display of choose (similar to the switch statement in programming languages)
Copy codeThe Code is as follows: <xsl: choose>
<Xsl: when test = "name () = 'pig'">
<I> this is a pig </I>
</Xsl: when>
<Xsl: otherwise>
<I> this is not a pig </I>
</Xsl: otherwise>
</Xsl: choose>
5) xsl: value-of displays the value of the selected node or attribute.
Select sub-node price
<Xsl: value-of select = "pets/*/price"/>
Select weight
<Xsl: value-of select = "pets/*/@ weight"/>
6) xsl: attribute: Construct the attributes of an xml node.
Used to add attributes to a node, for example:
<Font>
<Xsl: attribute name = "color"> <xsl: value-of select = "pets/*/@ color"/> </xsl: attribute>
</Font>
<Font color = "red"> </font>
7) xsl: apply-templates Application Template
If the xml file structure is complex, you can define multiple templates, and then use the <xsl: apply-templates> label Application template, xsl: apply-templates you can specify the select = "xpath" attribute to select the template of the application, or do not specify select to select the template of the current node.
See the following sample xslt file pets-templates.xsl
Complete sample xsl file: pets. xsl
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: template match = "/">
<Html>
<Head>
<META http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> lovely pets </title>
<Style type = "text/css">
Ul {margin: 10px 0 10px 0; padding: 0; width: 400px; text-align: left ;}
Li {height: 60px; display: block; list-style: none; padding: 4px; border: 1px solid # f0f0f0; margin: 5px ;}
</Style>
</Head>
<Body>
<Center>
<H1> lovely pets <Ul>
<Xsl: for-each select = "pets/*">
<Li>
<Xsl: choose>
<Xsl: when test = "name () = 'Dog'">
<Xsl: attribute name = "src"> http://estar-tv.com/images/comprofiler/gallery/dog.gif </xsl: attribute>
</Xsl: when>
<Xsl: when test = "name () = 'pig'">
<Xsl: attribute name = "src"> http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg </xsl: attribute>
</Xsl: when>
<Xsl: otherwise>
<Xsl: attribute name = "src"> http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg? 1143660418 </xsl: attribute>
</Xsl: otherwise>
</Xsl: choose>
</Img>
<Font>
<Xsl: attribute name = "face"> Courier </xsl: attribute>
<Xsl: attribute name = "color">
<Xsl: value-of select = "@ color"/>
</Xsl: attribute>
<Xsl: value-of select = "name ()"/>
</Font> said: "<xsl: value-of select =" desc "/>"
Weight: <xsl: value-of select = "@ weight"/>
<Xsl: if test = "@ weight <10">
<P>
<I> its weight is less than 10 km </I>
</P>
</Xsl: if>
</Li>
</Xsl: for-each>
</Ul>
</Center>
</Body>
</Html>
</Xsl: template>
</Xsl: stylesheet>
The complete example file pets-templates.xsl:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheetversion = "1.0"
Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: template match = "/">
<Html>
<Head>
<META http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> lovely pets </title>
<Style type = "text/css">
Ul {margin: 10px 0 10px 0; padding: 0; width: 400px; text-align: left ;}
Li {height: 60px; display: block; list-style: none; padding: 4px; border: 1px solid # f0f0f0; margin: 5px ;}
</Style>
</Head>
<Body>
<Center>
<H1> lovely pets <Ul>
<Xsl: apply-templates select = "pets"/>
</Ul>
</Center>
</Body>
</Html>
</Xsl: template>
<Xsl: template match = "pets">
<Xsl: apply-templates select = "dog"> </xsl: apply-templates>
<Xsl: apply-templates select = "pig"> </xsl: apply-templates>
<Xsl: apply-templates select = "cat"> </xsl: apply-templates>
</Xsl: template>
<Xsl: template match = "dog">
<Xsl: for-each select = ".">
<Li>
<Xsl: attribute name = "src"> http://estar-tv.com/images/comprofiler/gallery/dog.gif </xsl: attribute>
</Img>
<Font>
<Xsl: attribute name = "face"> Courier </xsl: attribute>
<Xsl: attribute name = "color">
<Xsl: value-of select = "@ color"/>
</Xsl: attribute>
Dog
</Font> said: "<xsl: value-of select =" desc "/>"
Weight: <xsl: value-of select = "@ weight"/>
<Xsl: if test = "@ weight <10">
<P>
<I> its weight is less than 10 km </I>
</P>
</Xsl: if>
</Li>
</Xsl: for-each>
</Xsl: template>
<Xsl: template match = "pig">
<Xsl: for-each select = ".">
<Li>
<Xsl: attribute name = "src"> http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg </xsl: attribute>
</Img>
<Font>
<Xsl: attribute name = "face"> Courier </xsl: attribute>
<Xsl: attribute name = "color">
<Xsl: value-of select = "@ color"/>
</Xsl: attribute>
Pig
</Font> said: "<xsl: value-of select =" desc "/>"
Weight: <xsl: value-of select = "@ weight"/>
<Xsl: if test = "@ weight <10">
<P>
<I> its weight is less than 10 km </I>
</P>
</Xsl: if>
</Li>
</Xsl: for-each>
</Xsl: template>
<Xsl: template match = "cat">
<Xsl: for-each select = ".">
<Li>
<Xsl: attribute name = "src"> http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg? 1143660418 </xsl: attribute>
</Img>
<Font>
<Xsl: attribute name = "face"> Courier </xsl: attribute>
<Xsl: attribute name = "color">
<Xsl: value-of select = "@ color"/>
</Xsl: attribute>
Cat
</Font> said: "<xsl: value-of select =" desc "/>"
Weight: <xsl: value-of select = "@ weight"/>
<Xsl: if test = "@ weight <10">
<P>
<I> its weight is less than 10 km </I>
</P>
</Xsl: if>
</Li>
</Xsl: for-each>
</Xsl: template>
</Xsl: stylesheet>
In c #. net, XslCompiledTransform can be used to convert xml documents, but this class has been marked as obsolete by Microsoft. It is better not to use it any more. The following code example:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Using System. Xml;
Namespace UseXslt
{
Class Program
{
Static void Main (string [] args)
{
// Declare the transform class instance
System. Xml. Xsl. Compile compiledtransform trans = new System. Xml. Xsl. Compile compiledtransform ();
String effectfile = @ "X: \ about.net \ System. Xml \ example \ pets. xsl ";
Using (StreamReader rdr = new StreamReader (xsltFile ))
{
Using (XmlReader xmlRdr = XmlReader. Create (rdr ))
{
// Load the xsl File
Trans. Load (xmlRdr );
}
}
String inputFile = @ "X: \ about.net \ System. Xml \ example \ pets. xml ";
String outputFile = @ "X: \ about.net \ System. Xml \ example \ pets-out.htm ";
// Convert the source file and output it to the output file outputFile
Trans. Transform (inputFile, outputFile );
}
}
}
Note that the file converted from XslCompiledTransform is in html format, this class automatically adds an unclosed meta tag <META http-equiv = "Content-Type" content = "text/html; charset = UTF-8 ">; Microsoft has helped us think too much.
Xslt can also specify parameters and define variables. For more information, see related documents.