合并两个XML文件在ASP.NET XSLT [英] Merge two xml files with xslt in ASP.NET
问题描述
我是新来的XSLT,我需要使用XSLT两个XML的合二为一。作为一个初步的锻炼,我创建了三个文件,file1.xml,file2.xml和transform.xslt,并推断出了如何通过file1.xml直接运行合并这些。这是我做的:
I'm new to xslt, and I need to combine two xmls into one using xslt. As an initial exercise, I created three files, file1.xml, file2.xml, and transform.xslt, and figured out how to merge those by running file1.xml directly. This is how I did it:
在file1.xml:
In file1.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="transform.xslt"?>
<stuff>
...
</stuff>
在file2.xml:
In file2.xml:
<morestuff>
...
</morestuff>
在transform.xslt:
In transform.xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:custom="custom-functions">
<xsl:output method="xml" indent="yes" version="1.0" encoding="ISO-8859-1"/>
<xsl:variable name="file2" select="document('file2.xml')" />
<xsl:doing-stuff>
...
<!-- contains references to $file2 -->
...
</xsl:doing-stuff>
</xsl:stylesheet>
不过,我需要能够通过我的C#ASP.NET code要做到这一点,并使用给定的XML的字符串而不是XML的文件。像这样:
But I need to be able to do this through my C# ASP.NET code, and using given xml strings instead of xml files. Like so:
public string MergeXmls(string xml1, string xml2){
string mergedXml;
var xsltPath = HttpContext.Current.Server.MapPath("transform.xslt");
//???
return mergedXml;
}
我怎样才能做到这一点?我知道我需要删除到文件引用(file2.xml')
从transform.xslt,但我不知道从哪里里去。
How can I make this happen? I know I need to remove the references to document('file2.xml')
from transform.xslt, but I don't know where to go from there.
推荐答案
这可以使用的 XML preloadedResolver
类。这使您可以将用于解析文件preLOAD实体
在XSLT的功能。
This can be accomplished using the XmlPreloadedResolver
class. This allows you to preload entities that will be used to resolve the document
function in the XSLT.
唯一需要注意的是,URI的在文件
函数被视为相对于XSLT文档本身的基础URI中指定,所以你必须使用稍微令人费解方法通过加载它的XmlReader
,以便您可以重写基URI。
The only caveat is that the URI's specified in the document
function are treated as relative to the base URI of the XSLT document itself, so you have to use a slightly convoluted method to load it through an XmlReader
so you can override the base URI.
public string MergeXml(string xml1, string xml2)
{
XslCompiledTransform xslt = new XslCompiledTransform();
XmlDocument xsltDoc = new XmlDocument();
// Load the XSLT file through XmlReader to override the base URI.
using( StreamReader reader = File.OpenText(HttpContext.Current.Server.MapPath("transform.xslt")) )
using( XmlReader xmlReader = XmlReader.Create(reader, null, "file:///transform.xslt") )
{
xsltDoc.Load(xmlReader);
}
// Use XsltSettings to enable the use of the document() function.
xslt.Load(xsltDoc, new XsltSettings(true, false), null);
// Load the first XML file into a document
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml1);
// Create the resolver and add the second file to it.
XmlPreloadedResolver resolver = new XmlPreloadedResolver();
resolver.Add(new Uri("file:///file2.xml"), xml2);
using( StringWriter writer = new StringWriter() )
using( XmlWriter xmlWriter = XmlWriter.Create(writer) )
{
// Pass the resolver to the transform
xslt.Transform(doc, null, xmlWriter, resolver);
return writer.ToString();
}
}
的 XML preloadedResolver
类仅在.NET 4.0中提供更高的,但如果您使用的是较早的版本仍然可以从派生自定义类 System.Xml.XmlResolver
和实施自己相似的功能。
The XmlPreloadedResolver
class is only available in .Net 4.0 and higher, but if you're using an earlier version you can still derive a custom class from System.Xml.XmlResolver
and implement similar functionality yourself.
这篇关于合并两个XML文件在ASP.NET XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!