gpt4 book ai didi

c# - 如何解析 XSL 包含在从字符串加载 XSL 的转换中?

转载 作者:太空狗 更新时间:2023-10-29 21:11:28 29 4
gpt4 key购买 nike

.NET 2.0/VS2005

我正在尝试使用 XslCompiledTransform类来执行 XSL 转换。我有两个 XSL 文件,第一个文件以 <xsl:include> 的形式包含对另一个文件的引用。声明:

主.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="Included.xsl" />
...
...
</xsl:stylesheet>

现在,如果我可以将“Main.xsl”文件本身作为 URI 加载,我的转换代码将非常简单:

// This is a function that works. For demo only.
private string Transform(string xslFileURI)
{
XslCompiledTransform xslt = new XslCompiledTransform();

// This load works just fine, if I provide the path to "Main.xsl".
// The xsl:include is automatically resolved.
xslTransform.Load(xslFileURI);

StringWriter sw = new StringWriter();
xslt.Transform(Server.MapPath("~/XML/input.xml"), null, sw);
return sw.ToString();
}

问题是我收到 Main.xsl 文件的内容作为一个字符串,并且需要将该字符串作为 XmlReader/IXpathNavigable 加载. 此时这是必要的限制。当我尝试使用 XmlReader/XpathDocument 执行相同操作时,它失败了,因为代码在 C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ 中查找“Included.xsl”文件夹!显然,XmlResolver无法解析相对 URL,因为它只接收一个字符串作为输入 XSL。

我在这个方向上的努力是这样的:

// This doesn't work! Halp!
private string Transform(string xslContents)
{
XslCompiledTransform xslt = new XslCompiledTransform();
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

//METHOD 1: This method does not work.
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;
XmlReader xR = XmlReader.Create(new StringReader(xslContents), settings);
xslt.Load(xR); // fails

// METHOD 2: Does not work either.
XPathDocument xpDoc = new XPathDocument(new StringReader(xslContents));
xslt.Load(xpDoc, new XsltSettings(true, true), resolver); //fails.

StringWriter sw = new StringWriter();
xslt.Transform(Server.MapPath("~/XML/input.xml"), null, sw);
return sw.ToString();
}

我尝试使用 ResolveUri XmlUrlResolver 的方法获取 Stream引用要包含的 XSL 文件,但对如何使用此 Stream 感到困惑。 IOW,我怎么告诉 XslCompiledTransform除了 Main.xsl XmlReader 之外还使用此流的对象:

Uri mainURI = new Uri(Request.PhysicalApplicationPath + "Main.xsl");
Uri uri = resolver.ResolveUri(mainURI, "Included.xsl");

// I can verify that the Included.xsl file loads in the Stream below.
Stream s = resolver.GetEntity(uri, null, typeof(Stream)) as Stream;

// How do I use this Stream in the function above??


任何帮助是极大的赞赏。很抱歉发了这么长的帖子!

供您引用,Exception StackTrace 如下所示:

[FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Included.xsl'.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +328
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) +113
System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +78
System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +51
System.Xml.Xsl.Xslt.XsltLoader.CreateReader(Uri uri, XmlResolver xmlResolver) +22
System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(Uri uri, Boolean include) +33
System.Xml.Xsl.Xslt.XsltLoader.LoadInclude() +349
System.Xml.Xsl.Xslt.XsltLoader.LoadRealStylesheet() +704
System.Xml.Xsl.Xslt.XsltLoader.LoadDocument() +293
System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include) +173

最佳答案

使用自定义的 XmlUrlResolver

class MyXmlUrlResolver : XmlUrlResolver
{
public override Uri ResolveUri(Uri baseUri, string relativeUri)
{
if (baseUri != null)
return base.ResolveUri(baseUri, relativeUri);
else
return base.ResolveUri(new Uri("http://mypath/"), relativeUri);
}
}

并在XslCompiledTransform的加载函数中使用它,

resolver=new MyXmlUrlResolver();
xslt.Load(xR,null,resolver);

关于c# - 如何解析 XSL 包含在从字符串加载 XSL 的转换中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/995591/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com