当前位置:编程学习 > C#/ASP.NET >>

Extending XSLT using C# 使用自定义函数

在xslt中用自定义的函数

1. xml文件不变

2. xslt文件要声明一个namespace,2种引用,select和非select需要加{},注意加""

3 随便声明一个类

4 transform函数要使用一个argumentlist的AddExtensionObject,指定上面的Namespace(就关联起来了)

-------------------1-----------------

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee>
    <FirstName>Pierre-Emmanuel</FirstName>
    <LastName>Dautreppe</LastName>
  </Employee>
</Employees>

-------------------2----------------------

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:myUtils="pda:MyUtils">
  <xsl:template match="/Employees">
    <Employees>
      <xsl:for-each select="Employee">
        <Employee>
          <xsl:value-of select="myUtils:FormatName(FirstName,LastName)" />
        </Employee>
        <br></br>
        <a href ="{myUtils:GetLink()}default.aspx">abc.com</a>
      </xsl:for-each>
    </Employees>
  </xsl:template>
</xsl:stylesheet>

------------3-----------------------------

public class MyXslExtension
{
    public string FormatName(string firstName,string name)
    {
        return name + ", " + firstName;
    }
    public string GetLink()
    {
        return "http://www.anypromo.com/";
    }
}

----------4---------------------------------

           XsltArgumentList arguments = new XsltArgumentList();

            arguments.AddExtensionObject("pda:MyUtils", new MyXslExtension());

            StringWriter output = new StringWriter();
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(Server.MapPath("a.xslt"));
            transform.Transform(Server.MapPath("a.xml"), arguments, output);
            Response.Write(output.ToString());

    
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,