我有这个 Saxon C# 方法,它接受一个输入 XML 节点,以及一个包含我在 MapForce 中生成的 XSLT 2.0 的字符串。此函数适用于一个 XML 输入节点,但我最近创建了一个具有多个输入的映射(即 2 个 xml 文件和 1 个输出)。所以我需要修改撒克逊代码以了解另一个输入节点。但是,XsltTransformer 类似乎只有一个“InitialContextNode”属性,没有其他东西可以指定额外的上下文节点。有谁熟悉如何让 XsltTransformer 类查看其他 XML 节点?谢谢。
public static string transform(string inputXML, string transformText) {
Processor processor = new Processor();
TextReader stringReader = new StringReader(inputXML);
XmlTextReader reader2 = new XmlTextReader(stringReader);
reader2.XmlResolver = null;
XdmNode input = processor.NewDocumentBuilder().Build(reader2);
XsltTransformer transformer = processor.NewXsltCompiler().Compile(new StringReader(transformText)).Load();
transformer.InputXmlResolver = null;
transformer.InitialContextNode = input;
Serializer serializer = new Serializer();
StringBuilder sb = new StringBuilder();
TextWriter txt = new StringWriter(sb);
serializer.SetOutputWriter(txt);
transformer.Run(serializer);
return sb.ToString();
}
所以基本上我需要的是这样的:
public static XElement transform(string transformText, params XElement[] inputXML) {
if (string.IsNullOrEmpty(transformText)) return null;
else {
Processor processor = new Processor();
List<XdmNode> nodes = new List<XdmNode>();
foreach (XElement input in inputXML) {
TextReader stringReader = new StringReader(input.ToString());
XmlTextReader reader2 = new XmlTextReader(stringReader);
reader2.XmlResolver = null;
nodes.Add(processor.NewDocumentBuilder().Build(reader2));
}
XsltTransformer transformer = processor.NewXsltCompiler().Compile(new StringReader(transformText)).Load();
transformer.InputXmlResolver = null;
transformer.InitialContextNode = nodes[0]; // How do I get it to see the other nodes?
Serializer serializer = new Serializer();
StringBuilder sb = new StringBuilder();
TextWriter txt = new StringWriter(sb);
serializer.SetOutputWriter(txt);
transformer.Run(serializer);
return XElement.Parse(sb.ToString());
}
}
带有 2 个输入的 XSLT 代码供参考: http: //pastebin.com/04ZTRe6m