本文主要演示xslt调用自定义的函数,此处用C#作为编写自定义函数的语言,用C#去完成xslt对xml的筛选功能。
CSharpFunctions.xslt
<?xml version="1.0" encoding="UTF-8" ?><?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<msxsl:script implements-prefix="csfun" language="C#">
<![CDATA[
public bool CheckDate(string target)
{
DateTime dtTarget;
if (DateTime.TryParse(target, out dtTarget))
{
DateTime dtMinDate = new DateTime(1900, 1, 1);
DateTime dtMaxDate = new DateTime(2079, 6, 1);
if ((dtTarget - dtMinDate).Days > 0 && (dtTarget - dtMaxDate).Days < 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool CheckInt(string target)
{
int tmpInt;
if (int.TryParse(target, out tmpInt))
{
return true;
}
else
{
return false;
}
}
public bool CheckLong(string target)
{
long tmpLong;
if (long.TryParse(target, out tmpLong))
{
return true;
}
else
{
return false;
}
}
public bool CheckDouble(string target)
{
double tmpDouble;
if (double.TryParse(target, out tmpDouble))
{
return true;
}
else
{
return false;
}
}
public bool CheckDecimal(string target)
{
decimal tmpDecimal;
if (decimal.TryParse(target, out tmpDecimal))
{
return true;
}
else
{
return false;
}
}
public string Trim(string target)
{
if (target == null)
{
return null;
}
else
{
return target.Trim();
}
}
public string TrimSymbol(string target, string symbol)
{
if (target == null || symbol == null)
{
return null;
}
else
{
return target.Replace(symbol, string.Empty);
}
}
]]>
</msxsl:script>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<msxsl:script implements-prefix="csfun" language="C#">
<![CDATA[
public bool CheckDate(string target)
{
DateTime dtTarget;
if (DateTime.TryParse(target, out dtTarget))
{
DateTime dtMinDate = new DateTime(1900, 1, 1);
DateTime dtMaxDate = new DateTime(2079, 6, 1);
if ((dtTarget - dtMinDate).Days > 0 && (dtTarget - dtMaxDate).Days < 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool CheckInt(string target)
{
int tmpInt;
if (int.TryParse(target, out tmpInt))
{
return true;
}
else
{
return false;
}
}
public bool CheckLong(string target)
{
long tmpLong;
if (long.TryParse(target, out tmpLong))
{
return true;
}
else
{
return false;
}
}
public bool CheckDouble(string target)
{
double tmpDouble;
if (double.TryParse(target, out tmpDouble))
{
return true;
}
else
{
return false;
}
}
public bool CheckDecimal(string target)
{
decimal tmpDecimal;
if (decimal.TryParse(target, out tmpDecimal))
{
return true;
}
else
{
return false;
}
}
public string Trim(string target)
{
if (target == null)
{
return null;
}
else
{
return target.Trim();
}
}
public string TrimSymbol(string target, string symbol)
{
if (target == null || symbol == null)
{
return null;
}
else
{
return target.Replace(symbol, string.Empty);
}
}
]]>
</msxsl:script>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<msxsl:script implements-prefix="csfun" language="C#">
<![CDATA[
public bool CheckDate(string target)
{
DateTime dtTarget;
if (DateTime.TryParse(target, out dtTarget))
{
DateTime dtMinDate = new DateTime(1900, 1, 1);
DateTime dtMaxDate = new DateTime(2079, 6, 1);
if ((dtTarget - dtMinDate).Days > 0 && (dtTarget - dtMaxDate).Days < 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool CheckInt(string target)
{
int tmpInt;
if (int.TryParse(target, out tmpInt))
{
return true;
}
else
{
return false;
}
}
public bool CheckLong(string target)
{
long tmpLong;
if (long.TryParse(target, out tmpLong))
{
return true;
}
else
{
return false;
}
}
public bool CheckDouble(string target)
{
double tmpDouble;
if (double.TryParse(target, out tmpDouble))
{
return true;
}
else
{
return false;
}
}
public bool CheckDecimal(string target)
{
decimal tmpDecimal;
if (decimal.TryParse(target, out tmpDecimal))
{
return true;
}
else
{
return false;
}
}
public string Trim(string target)
{
if (target == null)
{
return null;
}
else
{
return target.Trim();
}
}
public string TrimSymbol(string target, string symbol)
{
if (target == null || symbol == null)
{
return null;
}
else
{
return target.Replace(symbol, string.Empty);
}
}
]]>
</msxsl:script>
</xsl:stylesheet>
包含C#自定义函数的xslt文件:
CSharpFunctions.xslt
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<msxsl:script implements-prefix="csfun" language="C#">
<![CDATA[
public bool CheckDate(string target)
{
DateTime dtTarget;
if (DateTime.TryParse(target, out dtTarget))
{
DateTime dtMinDate = new DateTime(1900, 1, 1);
DateTime dtMaxDate = new DateTime(2079, 6, 1);
if ((dtTarget - dtMinDate).Days > 0 && (dtTarget - dtMaxDate).Days < 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool CheckInt(string target)
{
int tmpInt;
if (int.TryParse(target, out tmpInt))
{
return true;
}
else
{
return false;
}
}
public bool CheckLong(string target)
{
long tmpLong;
if (long.TryParse(target, out tmpLong))
{
return true;
}
else
{
return false;
}
}
public bool CheckDouble(string target)
{
double tmpDouble;
if (double.TryParse(target, out tmpDouble))
{
return true;
}
else
{
return false;
}
}
public bool CheckDecimal(string target)
{
decimal tmpDecimal;
if (decimal.TryParse(target, out tmpDecimal))
{
return true;
}
else
{
return false;
}
}
public string Trim(string target)
{
if (target == null)
{
return null;
}
else
{
return target.Trim();
}
}
public string TrimSymbol(string target, string symbol)
{
if (target == null || symbol == null)
{
return null;
}
else
{
return target.Replace(symbol, string.Empty);
}
}
]]>
</msxsl:script>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<msxsl:script implements-prefix="csfun" language="C#">
<![CDATA[
public bool CheckDate(string target)
{
DateTime dtTarget;
if (DateTime.TryParse(target, out dtTarget))
{
DateTime dtMinDate = new DateTime(1900, 1, 1);
DateTime dtMaxDate = new DateTime(2079, 6, 1);
if ((dtTarget - dtMinDate).Days > 0 && (dtTarget - dtMaxDate).Days < 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public bool CheckInt(string target)
{
int tmpInt;
if (int.TryParse(target, out tmpInt))
{
return true;
}
else
{
return false;
}
}
public bool CheckLong(string target)
{
long tmpLong;
if (long.TryParse(target, out tmpLong))
{
return true;
}
else
{
return false;
}
}
public bool CheckDouble(string target)
{
double tmpDouble;
if (double.TryParse(target, out tmpDouble))
{
return true;
}
else
{
return false;
}
}
public bool CheckDecimal(string target)
{
decimal tmpDecimal;
if (decimal.TryParse(target, out tmpDecimal))
{
return true;
}
else
{
return false;
}
}
public string Trim(string target)
{
if (target == null)
{
return null;
}
else
{
return target.Trim();
}
}
public string TrimSymbol(string target, string symbol)
{
if (target == null || symbol == null)
{
return null;
}
else
{
return target.Replace(symbol, string.Empty);
}
}
]]>
</msxsl:script>
</xsl:stylesheet>
调用C#自定义函数的xslt文件:
CSRCNAV.xslt
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:include href="CSharpFunctions.xslt"/>
<xsl:template match="/">
<CSRCNAVSet>
<xsl:for-each select="//table[@id='tablesorter-instance']/tbody/tr">
<CSRCNAV>
<Symbol>
<xsl:value-of select="td[position()=2]"/>
</Symbol>
<Name>
<xsl:value-of select="csfun:Trim(td[position()=3])"/>
</Name>
<xsl:if test="csfun:CheckDecimal(td[position()=4])">
<ClosePrice>
<xsl:value-of select="td[position()=4]"/>
</ClosePrice>
</xsl:if>
<EffectiveDate>
<xsl:value-of select="td[position()=6]"/>
</EffectiveDate>
</CSRCNAV>
</xsl:for-each>
</CSRCNAVSet>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:csfun="urn:csharp-functions" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:include href="CSharpFunctions.xslt"/>
<xsl:template match="/">
<CSRCNAVSet>
<xsl:for-each select="//table[@id='tablesorter-instance']/tbody/tr">
<CSRCNAV>
<Symbol>
<xsl:value-of select="td[position()=2]"/>
</Symbol>
<Name>
<xsl:value-of select="csfun:Trim(td[position()=3])"/>
</Name>
<xsl:if test="csfun:CheckDecimal(td[position()=4])">
<ClosePrice>
<xsl:value-of select="td[position()=4]"/>
</ClosePrice>
</xsl:if>
<EffectiveDate>
<xsl:value-of select="td[position()=6]"/>
</EffectiveDate>
</CSRCNAV>
</xsl:for-each>
</CSRCNAVSet>
</xsl:template>
</xsl:stylesheet>
调用目标xml文件:
target.xml
<?xml version="1.0" encoding="utf-8"?>
<!--此处主要显示文件的结构,完整的内容请查看源代码中的文件-->
<Root>
<table id="tablesorter-instance" class="tablesorter" width="100%" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
<th>序号</th>
<th>基金代码</th>
<th>基金简称</th>
<th>
当日净值
</th>
<th>
累计净值
</th>
<th>估值日期 </th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">688888</td>
<td align="center">
<a href="/web/fund_detail.fund?fundId=808080802fdcf1fc012ffd2ffba96c01&classification=2030-1030" target="_blank">
浙商聚潮产业成长股票
</a>
</td>
<td align="center">0.840</td>
<td align="center">0.840</td>
<td align="center">2012-01-12</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">710001</td>
<td align="center">
<a href="/web/fund_detail.fund?fundId=80808080317379bd01328b3fc41e73eb&classification=2030-1030" target="_blank">
富安达优势成长股票
</a>
</td>
<td align="center">0.9561</td>
<td align="center">0.9561</td>
<td align="center">2012-01-12</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">162006</td>
<td align="center">
<a href="/web/fund_detail.fund?fundId=4028928d1ab44ebf011ab44ec4a400b7&classification=2030-1030" target="_blank">
长城久富股票(LOF)
</a>
</td>
<td align="center">0.9407</td>
<td align="center">3.3315</td>
<td align="center">2012-01-12</td>
</tr>
</tbody>
</table>
</Root>
<!--此处主要显示文件的结构,完整的内容请查看源代码中的文件-->
<Root>
<table id="tablesorter-instance" class="tablesorter" width="100%" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
<th>序号</th>
<th>基金代码</th>
<th>基金简称</th>
<th>
当日净值
</th>
<th>
累计净值
</th>
<th>估值日期 </th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">688888</td>
<td align="center">
<a href="/web/fund_detail.fund?fundId=808080802fdcf1fc012ffd2ffba96c01&classification=2030-1030" target="_blank">
浙商聚潮产业成长股票
</a>
</td>
<td align="center">0.840</td>
<td align="center">0.840</td>
<td align="center">2012-01-12</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">710001</td>
<td align="center">
<a href="/web/fund_detail.fund?fundId=80808080317379bd01328b3fc41e73eb&classification=2030-1030" target="_blank">
富安达优势成长股票
</a>
</td>
<td align="center">0.9561</td>
<td align="center">0.9561</td>
<td align="center">2012-01-12</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">162006</td>
<td align="center">
<a href="/web/fund_detail.fund?fundId=4028928d1ab44ebf011ab44ec4a400b7&classification=2030-1030" target="_blank">
长城久富股票(LOF)
</a>
</td>
<td align="center">0.9407</td>
<td align="center">3.3315</td>
<td align="center">2012-01-12</td>
</tr>
</tbody>
</table>
</Root>
C#代码:
1) 导出一个Format后的文件output.xml:
View Code
/// <summary>
/// 输出Format后的xml文件output.xml
/// </summary>
private static void OutputFormatXML()
{
string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "target.xml");
string xsltPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSRCNAV.xslt");
string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
StringBuilder sbXml = new StringBuilder();
StringWriter stringWriter = new StringWriter(sbXml);
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
//设置可以执行脚本函数
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
settings.EnableScript = true;
//设置xslt可以包含外部的xslt文件
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(xsltPath, settings, resolver);
xslCompiledTransform.Transform(xmlDoc, xmlTextWriter);
XmlDocument xmlFormatDoc = new XmlDocument();
xmlFormatDoc.LoadXml(sbXml.ToString());
xmlFormatDoc.Save(outputPath);
}
/// 输出Format后的xml文件output.xml
/// </summary>
private static void OutputFormatXML()
{
string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "target.xml");
string xsltPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSRCNAV.xslt");
string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
StringBuilder sbXml = new StringBuilder();
StringWriter stringWriter = new StringWriter(sbXml);
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
//设置可以执行脚本函数
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
settings.EnableScript = true;
//设置xslt可以包含外部的xslt文件
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(xsltPath, settings, resolver);
xslCompiledTransform.Transform(xmlDoc, xmlTextWriter);
XmlDocument xmlFormatDoc = new XmlDocument();
xmlFormatDoc.LoadXml(sbXml.ToString());
xmlFormatDoc.Save(outputPath);
}
2) 再加上一个schema文件还可以构造成想要的DataTable:
Schema文件:
CSRCNAV.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="CSRCNAVSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="CSRCNAVSet" msdata:MainDataTable="CSRCNAV" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="CSRCNAV">
<xs:complexType>
<xs:sequence>
<xs:element name="Symbol" type="xs:string" minOccurs="0" />
<xs:element name="ClosePrice" type="xs:decimal" minOccurs="0" />
<xs:element name="BidPrice" type="xs:decimal" minOccurs="0" />
<xs:element name="OfferPrice" type="xs:decimal" minOccurs="0" />
<xs:element name="EffectiveDate" type="xs:dateTime" minOccurs="0" />
<xs:element name="DataSource" type="xs:string" default="csrc" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema id="CSRCNAVSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="CSRCNAVSet" msdata:MainDataTable="CSRCNAV" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="CSRCNAV">
<xs:complexType>
<xs:sequence>
<xs:element name="Symbol" type="xs:string" minOccurs="0" />
<xs:element name="ClosePrice" type="xs:decimal" minOccurs="0" />
<xs:element name="BidPrice" type="xs:decimal" minOccurs="0" />
<xs:element name="OfferPrice" type="xs:decimal" minOccurs="0" />
<xs:element name="EffectiveDate" type="xs:dateTime" minOccurs="0" />
<xs:element name="DataSource" type="xs:string" default="csrc" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
C#代码:
View Code
private static DataTable OutputDataTable()
{
string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "target.xml");
string xsltPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSRCNAV.xslt");
string schemaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSRCNAV.xsd");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
//设置可以执行脚本函数
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
settings.EnableScript = true;
//设置xslt可以包含外部的xslt文件
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
MemoryStream memoryStream = new MemoryStream();
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(xsltPath, settings, resolver);
xslCompiledTransform.Transform(xmlDoc, null, memoryStream);
using (DataTable dt = new DataTable())
{
dt.ReadXmlSchema(schemaPath);
memoryStream.Position = 0;
dt.ReadXml(memoryStream);
return dt;
}
}
{
string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "target.xml");
string xsltPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSRCNAV.xslt");
string schemaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CSRCNAV.xsd");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
//设置可以执行脚本函数
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
settings.EnableScript = true;
//设置xslt可以包含外部的xslt文件
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
MemoryStream memoryStream = new MemoryStream();
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(xsltPath, settings, resolver);
xslCompiledTransform.Transform(xmlDoc, null, memoryStream);
using (DataTable dt = new DataTable())
{
dt.ReadXmlSchema(schemaPath);
memoryStream.Position = 0;
dt.ReadXml(memoryStream);
return dt;
}
}
源代码下载