xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<circles>
<circle>
<radius>15</radius>
</circle>
<circle>
<radius>18</radius>
</circle>
</circles>
xslt 文件
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns="urn:myobject">
<xsl:template match="circles/circle">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="radius">
<circumference>
<xsl:value-of select="myns:getcircumference(.)"/>
</circumference>
</xsl:template>
</xsl:stylesheet>
主程序
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace extensionobject
{
class Program
{
private static string inputfile = @"E:\c#\c#_windows程序设计\extensionobject\extensionobject\circles.xml";
private static string stylefile = @"E:\c#\c#_windows程序设计\extensionobject\extensionobject\circles.xslt";
private static string resultfile = @"E:\c#\c#_windows程序设计\extensionobject\extensionobject\result.xml";
static void Main(string[] args)
{
calculate mycalculate = new calculate();
XsltArgumentList arglist = new XsltArgumentList();
arglist.AddExtensionObject("urn:myobject", mycalculate); //前一参数指定命名空间
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(stylefile);
XPathDocument inputdoc = new XPathDocument(inputfile);
XmlTextWriter xtw = new XmlTextWriter(resultfile, null);
transformer.Transform(inputdoc, arglist, xtw);
xtw.Close();
}
}
public class calculate
{
public double getcircumference(double radius)
{
return Math.PI * 2 * radius;
}
}
}
评论