在 Visual Studio 2010 中使用扩展对象调试 XSLT

发布于 2024-09-03 00:05:23 字数 1998 浏览 5 评论 0原文

我目前正在开发一个涉及大量 XSLT 转换的项目,我确实需要一个调试器(我有 1000 多行长的 XSLT,但它们不是我写的 :-)。

该项目是用 C# 编写的,并使用扩展对象:

xslArg.AddExtensionObject("urn:<obj>", new <Obj>());

据我所知,在这种情况下,Visual Studio 是唯一可以帮助我逐步调试转换的工具。由于扩展对象,静态调试器没有用(当它到达引用其名称空间的元素时,它会抛出错误)。幸运的是,我发现这个线程给了我一个起点(至少我知道它可以做到)。

在搜索 MSDN 后,我找到了可以进行转换的标准。它们被列出 此处。简而言之:

  • XML 和 XSLT 必须通过具有 IXmlLineInfo 接口 (XmlReader & co.) 的类来加载,该接口是
  • XSLTCompiledTransform 中使用的 XML 解析器 构造函数是基于文件的(XmlUriResolver 应该可以工作)。
  • 样式表应该位于本地计算机或内部网上(?)

据我所知,我符合所有这些标准,但它仍然不起作用。相关代码示例发布如下:

// [...]

xslTransform = new XslCompiledTransform(true);

xslTransform.Load(XmlReader.Create(new StringReader(contents)), null, new BaseUriXmlResolver(xslLocalPath));

// [...]

// I already had the xml loaded in an xmlDocument 
// so I have to convert to an XmlReader
XmlTextReader r = new XmlTextReader(new StringReader(xmlDoc.OuterXml));

XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddExtensionObject("urn:[...]", new [...]());
xslTransform.Transform(r, xslArg, context.Response.Output);

我真的不明白我做错了什么。我检查了两个 XmlReader 对象上的接口,它们实现了所需的接口。此外,BaseUriXmlResolver 继承自 XmlUriResolver,并且样式表存储在本地。下面的屏幕截图是我在进入 Transform 函数时得到的结果。首先,我可以在逐步执行参数(在模板匹配上)后看到样式表代码,我得到:

< img src="https://i.sstatic.net/rNihC.jpg" alt="当我进入样式表时遇到的错误">

如果有人知道为什么它不起作用或者有其他方法让它工作,我将非常感激:)。

谢谢,
亚历克斯

I'm currently working on a project that involves a lot of XSLT transformations and I really need a debugger (I have XSLTs that are 1000+ lines long and I didn't write them :-).

The project is written in C# and makes use of extension objects:

xslArg.AddExtensionObject("urn:<obj>", new <Obj>());

From my knowledge, in this situation Visual Studio is the only tool that can help me debug the transformations step-by-step. The static debugger is no use because of the extension objects (it throws an error when it reaches elements that reference their namespace). Fortunately, I've found this thread which gave me a starting point (at least I know it can be done).

After searching MSDN, I found the criteria that makes stepping into the transform possible. They are listed here. In short:

  • the XML and the XSLT must be loaded via a class that has the IXmlLineInfo interface (XmlReader & co.)
  • the XML resolver used in the XSLTCompiledTransform constructor is file-based (XmlUriResolver should work).
  • the stylesheet should be on the local machine or on the intranet (?)

From what I can tell, I fit all these criteria, but it still doesn't work. The relevant code samples are posted below:

// [...]

xslTransform = new XslCompiledTransform(true);

xslTransform.Load(XmlReader.Create(new StringReader(contents)), null, new BaseUriXmlResolver(xslLocalPath));

// [...]

// I already had the xml loaded in an xmlDocument 
// so I have to convert to an XmlReader
XmlTextReader r = new XmlTextReader(new StringReader(xmlDoc.OuterXml));

XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddExtensionObject("urn:[...]", new [...]());
xslTransform.Transform(r, xslArg, context.Response.Output);

I really don't get what I'm doing wrong. I've checked the interfaces on both XmlReader objects and they implement the required one. Also, BaseUriXmlResolver inherits from XmlUriResolver and the stylesheet is stored locally. The screenshot below is what I get when stepping into the Transform function. First I can see the stylesheet code after stepping through the parameters (on template-match), I get this:

The error I get when I step into the stylesheet

If anyone has any idea why it doesn't work or has an alternative way of getting it to work I'd be much obliged :).

Thanks,
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-09-10 00:05:23

我不确定扩展对象的使用情况,但据我了解,您的问题是在 VS2010 中调试代码中的 XSLT 转换。
这是我们用来调试 XSLT 转换的函数:

 public string ApplyTransformation(string inputFilePath, string xsltFileContent)
    {
        XslCompiledTransform transform = new XslCompiledTransform(debugEnabled);

        File.WriteAllText(xsltTempFilePath,xsltFileContent);
        transform.Load(xsltTempFilePath, XsltSettings.TrustedXslt, new XmlUrlResolver());

        XmlReader reader = XmlReader.Create(inputFilePath);
        StringWriter output = new StringWriter();
        XmlWriter writer =  XmlWriter.Create(output,transform.OutputSettings);
        transform.Transform(reader,writer);
        return output.ToString();
    }

不幸的是,有一个 bug 将使您的调试体验比 VS2008 更差。

I'm not sure about usage of extension objects but as I understand your problem is with debugging of XSLT transformation in code in VS2010.
Here is the function that we use to debug XSLT transformation:

 public string ApplyTransformation(string inputFilePath, string xsltFileContent)
    {
        XslCompiledTransform transform = new XslCompiledTransform(debugEnabled);

        File.WriteAllText(xsltTempFilePath,xsltFileContent);
        transform.Load(xsltTempFilePath, XsltSettings.TrustedXslt, new XmlUrlResolver());

        XmlReader reader = XmlReader.Create(inputFilePath);
        StringWriter output = new StringWriter();
        XmlWriter writer =  XmlWriter.Create(output,transform.OutputSettings);
        transform.Transform(reader,writer);
        return output.ToString();
    }

Unfortunately, there is a bug with VS2010 XSLT debugger which will make your debugging experience worse than in VS2008.

—━☆沉默づ 2024-09-10 00:05:23

考虑使用 XML Spy XSLT 调试器进行调试。它一直对我有用。

Consider debugging using XML Spy XSLT debugger. It works for me all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文