我正在使用撒克逊人9.8。我想在C#中编写一个转换函数,如下所示。所以我有
`using Saxon.Api;
private string Transform(Stream xmlStream, string transform)
{
}`
请帮我填写此功能
答案 0 :(得分:1)
我认为你应该至少尝试自己编写代码,当你遇到问题时,请在互联网上查找可能的答案。只有当你不能通过这种方式解决我们的问题而不是问一个特定的问题时。我不打算为你编写完整的代码。但我可以在这里分享我如何使用Saxon API进行XSLT转换:
public static String TransfromXMLWithXSLT2(String i_xmlData, String i_xsltCode)
{
Processor xsltProcessor = new Processor();
DocumentBuilder documentBuilder = xsltProcessor.NewDocumentBuilder();
documentBuilder.BaseUri = new Uri("file://");
XdmNode xdmNode = documentBuilder.Build(new StringReader(i_xmlData));
XsltCompiler xsltCompiler = xsltProcessor.NewXsltCompiler();
XsltExecutable xsltExecutable = xsltCompiler.Compile(new StringReader(i_xsltCode));
XsltTransformer xsltTransformer = xsltExecutable.Load();
xsltTransformer.InitialContextNode = xdmNode;
using (StringWriter stringWriter = new StringWriter()) {
Serializer serializer = new Serializer();
serializer.SetOutputWriter(stringWriter);
xsltTransformer.Run(serializer);
return stringWriter.ToString();
}
}
我在转换类中使用了一个开关来支持XSLT1.0。对于XSLT1.0,我更喜欢.Net功能,仅用于我的代码中的信息:
public static String TransfromXMLWithXSLT1(String i_xmlData, String i_xsltCode)
{
XslCompiledTransform xslt = new XslCompiledTransform();
using (StringReader xsltStringReader = new StringReader(i_xsltCode)) {
using (XmlReader xsltXmlReader = XmlReader.Create(xsltStringReader)) {
xslt.Load(xsltXmlReader);
using (StringReader tableStringReader = new StringReader(i_xmlData)) {
using (XmlReader tableXmlReader = XmlReader.Create(tableStringReader)) {
using (StringWriter stringWriter = new StringWriter()) {
xslt.Transform(tableXmlReader, new XsltArgumentList(), stringWriter);
return stringWriter.ToString();
}
}
}
}
}
}
因此,如果您需要Streams / Readers而不是字符串,只需按照需要的方式更改代码!