如何在从String加载XSL的Transformation中解析XSL包含?

时间:2009-06-15 11:24:24

标签: c# xslt .net-2.0 include xslcompiledtransform

.NET 2.0 / VS2005

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

Main.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执行相同操作时,它会失败,因为代码会查找“Included.xsl”在C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\文件夹中!显然,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();
}

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

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

4 个答案:

答案 0 :(得分:5)

使用自定义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);

答案 1 :(得分:2)

我可能错过了显而易见的但有没有理由你不只是将Included.xsl的URI更改为真正的URL?如果您有权访问或使用字符串操作,这可以在XSL文档中完成吗?

答案 2 :(得分:2)

正如Gee的回答所提到的,你想要使用自定义XmlResolver(其中XmlUrlResolver已经派生),但是如果你也覆盖方法GetEntity,你可以解析主要的XSLT文档以有趣和有趣的方式。一个故意简单的示例,说明如何解析对 Included.xsl 的引用:

public class CustomXmlResolver : XmlResolver
{
    public CustomXmlResolver() { }

    public override ICredentials Credentials
    {
        set { }
    }

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        MemoryStream entityStream = null;

        switch (absoluteUri.Scheme)
        {
            case "custom-scheme":

                string absoluteUriOriginalString = absoluteUri.OriginalString;
                string ctgXsltEntityName = absoluteUriOriginalString.Substring(absoluteUriOriginalString.IndexOf(":") + 1);
                string entityXslt = "";

                // TODO: Replace the following with your own code to load data for referenced entities.
                switch (ctgXsltEntityName)
                {
                    case "Included.xsl":
                        entityXslt = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n  <xsl:template name=\"Included\">\n\n  </xsl:template>\n</xsl:stylesheet>";
                        break;
                }

                UTF8Encoding utf8Encoding = new UTF8Encoding();
                byte[] entityBytes = utf8Encoding.GetBytes(entityXslt);
                entityStream = new MemoryStream(entityBytes);

                break;
        }

        return entityStream;
    }

    public override Uri ResolveUri(Uri baseUri, string relativeUri)
    {
        // You might want to resolve all reference URIs using a custom scheme.
        if (baseUri != null)
            return base.ResolveUri(baseUri, relativeUri);
        else
            return new Uri("custom-scheme:" + relativeUri);
    }
}

当您加载 Main.xsl 文档时,您需要将相关代码更改为以下内容:

xslt.Load(xpDoc, new XsltSettings(true, true), new CustomXmlResolver());

以上示例基于我在MSDN文章Resolving the Unknown: Building Custom XmlResolvers in the .NET Framework中提到的信息。

答案 3 :(得分:0)

我已经成功使用内存中的所有内容进行转换:

拥有包含以下内容的xslt包括:

import href =“Common.xslt”和 import href =“Xhtml.xslt”

    private string Transform(string styleSheet, string xmlToParse)
            {
                XslCompiledTransform xslt = new XslCompiledTransform();

                MemoryResourceResolver resolver = new MemoryResourceResolver();            


                XmlTextReader xR = new XmlTextReader(new StringReader(styleSheet));           

                xslt.Load(xR, null, resolver);

                StringWriter sw = new StringWriter();                


                using (var inputReader = new StringReader(xmlToParse))
                {
                    var input = new XmlTextReader(inputReader);
                    xslt.Transform(input,
                                        null,
                                        sw);
                }

                return sw.ToString();

            }     

    public class MemoryResourceResolver : XmlResolver
        {

            public override object GetEntity(Uri absoluteUri,
              string role, Type ofObjectToReturn)
            {
                if (absoluteUri.ToString().Contains("Common"))
                {
                    return new MemoryStream(Encoding.UTF8.GetBytes("Xml with with common data"));
                }

                if (absoluteUri.ToString().Contains("Xhtml"))
                {
                    return new MemoryStream(Encoding.UTF8.GetBytes("Xml with with xhtml data"));
                }         

                return "";
            }
        }

请注意,绝对所有内容都是字符串:styleSheet,xmlToParse以及“Common”和“Xhtml”导入的内容