在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());