正文

调用可扩展对象 xml2007-11-03 17:10:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/30724.html

分享到:

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;
        }
    }
}

阅读(1834) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册