Parsing examples of using XSLT to transform XML documents in. NET _ Practical Tips

Source: Internet
Author: User
Tags xpath xsl xsl file xslt
The style sheet file that the XSL can extend. You can format the display of the XML, or you can convert the XML to another format that you need.
Learning XSL must be familiar with XPath. XSL is as simple and powerful as XPath and easy to learn.
1. Since XSL can format the display style of XML, let's look at how to refer to the XSL file in XML
The following code example:
<?xml version= "1.0" encoding= "Utf-8"?>
<?xml-stylesheet type= "text/xsl" href= "url.xsl"?>
Simply add <?xml-stylesheet type= "text/xsl" href= "url.xsl" after the document declaration of the XML file?>
2. xsl format
XSL is also a standard XML file that starts with an XML document declaration, the root element must be Xsl:styleshee, and the root element must have a version attribute that specifies the XSL, and xmlns:xsl= "http://www.w3.org/1999/ Xsl/transform specifies the XSL namespace, 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 XSL essentials are as follows sample XML
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<?xml-stylesheet type= "text/xsl" href= "pets-templates.xsl"?>
<pets>
<pig color= "Blue" weight= ">"
<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= ">"
<price>80</price>
<desc>this is a green dog</desc>
</dog>
<cat color= "green" weight= ">"
<price>80</price>
<desc>this is a green cat</desc>
</cat>


<dog color= "Blue" weight= "ten" >
<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 above XML is displayed as follows through the XSL format:


1) xsl:template defines the transformation template that matches the node, and attributes Match= "XPath expression" to define the element that the template matches
The following defines a template that matches the root node

Copy Code code as follows:

<xsl:template match= "/" >
</xsl:template>

2) The Xsl:for-each loop shows the transformation of the select= "XPath expression" selection node (similar to a foreach statement in a programming language).
The following example selects the child elements below pets and loops through the names of the child elements:
Copy Code code as follows:

<xsl:for-each select= "/pets/*" >
<xsl:value-of select= "name ()"/>
</xsl:for-each>

3 xsl:if element Condition display node (if statement in similar programming language) Note that the less than number is greater than the < and > substitution
Copy Code code as follows:

<xsl:if test= "@weight <" >
<i>its weight is less than km</i>
</xsl:if>

4 xsl:choose Multiple branching condition display (similar to a switch statement in a programming language)
Copy Code code 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 Display the value of the selected node or attribute
Select child node Price
<xsl:value-of select= "Pets/*/price"/>
Select Properties Weight
<xsl:value-of select= "pets/*/@weight"/>
6) Xsl:attribute constructs 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>
Will output <font color= "Red" ></font>
7) Xsl:apply-templates Application Template
If the XML file structure is complex, you can define multiple template, and then use the <xsl:apply-templates> tag to apply the template, Xsl:apply-templates can specify properties select= "XPath" To select the template to apply, or select the template for the current node without specifying a select.
Consider the following sample XSLT file pets-templates.xsl
Complete sample XSL file: pets.xsl
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:template match= "/" >
<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>
<body>
<center>
<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 <" >
<p>
<i>its weight is less than km</i>
</p>
</xsl:if>


</li>
</xsl:for-each>
</ul>
</center>
</body>
</xsl:template>
</xsl:stylesheet>

Complete sample file pets-templates.xsl:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:template match= "/" >
<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>
<body>
<center>
<ul>
<xsl:apply-templates select= "Pets"/>
</ul>
</center>
</body>
</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 <" >
<p>
<i>its weight is less than 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 <" >
<p>
<i>its weight is less than 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 <" >
<p>
<i>its weight is less than km</i>
</p>
</xsl:if>
</li>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Using XslCompiledTransform to transform XML documents in C#.net, XslTransform can also be used, but this class has been marked as obsolete by Microsoft, and it is best not to use it again, as the following code example:
Copy Code code 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)
{
Declaring XslTransform class instances
System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform ();

String xsltfile = @ "X:\about.net\System.Xml\example\pets.xsl";
using (StreamReader RDR = new StreamReader (xsltfile))
{
using (XmlReader xmlrdr = Xmlreader.create (RDR))
{
Loading an 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 source file output to output file OutputFile
Trans. Transform (Inputfile, outputfile);
}
}
}

One thing to note is that a file converted using XslCompiledTransform is an HTML format that automatically adds an <meta META tag to the head tag of the HTML http-equiv= " Content-type "content=" text/html; Charset=utf-8 ">; Microsoft has helped us think too much.
XSLT can also specify parameters, define variables, and see related documentation for these areas.

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.