我如何使用XSLT在.NET? [英] How do I use XSLT in .NET?
问题描述
我将要转换的XML文档转换为基于可扩展样式语言转换另一个XML文档。我在哪里可以找到有关如何做到这一点的.NET好的教程?
我已经找到如何使用开源工具去做一些东西。但有关.NET框架是什么?只是一对夫妇的其他简单的问题...
-
可能有人请给我操作的XSLT秩序的快速和肮脏的解释呢?我还是有点困惑会发生什么?
-
是否有任何明确的.NET工具与XSLT文件的工作?我知道,与XSLT文件,XSD和XML文件时,你会得到一些XML下拉列表Visual Studio .NET中的主菜单上。我想这是好现在,但它会很高兴知道如果我有其他的选择。
-
我不打算实际转换文件...嗯,我想可扩展样式表将是一个文件,但我想导入XML字符串,它转换为另一个XML字符串,然后通过到了MVC设计模式的看法。我该怎么做?
1)可能有人请给我操作的XSLT秩序的快速和肮脏的解释呢?我还是有点困惑会发生什么?
块引用>但从使用的角度来看,只有一种操作:你抢了一些投入,XSLT引擎将其转换为输出
2)是为了与XSLT的工作任何明确的.Net工具?我知道,与XSLT的,XSD的,和XML文件时,你会得到一些XML下拉列表Visual Studio .NET中的主菜单上。我想这是确定了,但如果我有其他的选择这将是很好知道的。
块引用>随着
XslCompiledTransform
你可以申请您的XSL转换。
3)我不打算真正改变文件...嗯,我想可扩展样式表将是一个文件,但我要导入XML字符串,将其转换为另一个XML字符串,然后通过到了MVC设计模式的看法。每个人之前尝试任何疯狂的喜欢吗?如果是这样,有什么建议?
块引用>我上面提到的
XslCompiledTransform
类可以对数据流直接工作,或者的XmlReader
和的XmlWriter
的对象,所以你可以在内存中的整个事情,没有任何临时文件。下面是一个基本的例子:
//将XSL从文件转换
VAR变换=新XslCompiledTransform();
transform.Load(foo.xslt);//这是你输入的字符串
字符串输入= / * *嗒嗒/;//使XML阅读器出来的串
的XmlReader inputXmlReader;
使用(VAR inputReader =新StringReader(输入))
{
inputXmlReader = XmlReader.Create(inputReader);
}使用(作家=新的StringWriter())// prepare字符串作家输出
{
//如果你需要参数传递给XSLT ...
变参=新的XsltArgumentList();
args.AddParam(钥匙,金塔:XML名称空间的键,值); //应用转换给读者,它在我们的字符串作家写
transform.Transform(inputXmlReader,ARGS,作家); //从字符串作家输出字符串
返回writer.GetStringBuilder()的ToString()。
}
我在哪里可以找到有关如何做到这一点(...)好的教程?
块引用>如果您想学习XSLT语言本身,你可以看看这个previous问题:的如何开始使用XSLT的变换?。
I am going to be translating an XML document into another XML document based on an eXtensible Style Language Transformation. Where can I find good tutorials about how to do this in .NET?
I have found some stuff about how to do it using open source tools. But what about the .NET framework? Just a couple of other quick questions...
Could somebody please give me a quick and dirty explanation of the XSLT order of operations? I am still a little confused about what happens?
Are there any explicit .NET tools for working with XSLTs? I know that when working with XSLTs, XSDs, and XML files you get a little XML drop down list on the main menu of Visual Studio .NET. I guess that is Ok for now, but it would be nice to know if I had other options.
I am not going to be actually transforming files... Well, I guess the Extensible Stylesheet will be a file, but I want to import an XML string, transform it to another XML string, and then through that out to a view in the MVC design pattern. How can I do that?
解决方案1) could somebody please give me a quick and dirty explanation of the XSLT order of operations? I am still a little confused about what happens?
From the point of view of usage, there's only one operation: you grab some input, and the XSLT engine transforms it into an output.
2) are there any explicit .Net tools for working with XSLT's? I know that when working with XSLT's, XSD's, and XML files you get a little XML drop down list on the main menu of Visual studio .net. I guess that is ok for now, but it would be nice to know if I had other options.
With the
XslCompiledTransform
you can apply your XSL transformations.3) I am not going to be actually transforming files... Well, I guess the Extensible Style sheet will be a file, but I want to import a xml string, transform it to another xml string, and then through that out to a view in the MVC design pattern. Anybody every try anything crazy like that before? If so, any advice?
The
XslCompiledTransform
class I mentioned above can work directly on streams, orXmlReader
andXmlWriter
objects, so you can do the whole thing in memory, without any temporary files.Here's a basic example:
// Load the XSL transform from a file var transform = new XslCompiledTransform(); transform.Load("foo.xslt"); // This is your input string string input = /* blah */; // Make an XML reader out of the string XmlReader inputXmlReader; using(var inputReader = new StringReader(input)) { inputXmlReader = XmlReader.Create(inputReader); } using(writer = new StringWriter()) // prepare a string writer for the output { // if you need to pass arguments to the XSLT... var args = new XsltArgumentList(); args.AddParam("key", "urn:xml-namespace-of-key", "value"); // Apply the transformation to the reader and write it in our string writer transform.Transform(inputXmlReader, args, writer); // Retrieve the output string from the string writer return writer.GetStringBuilder().ToString(); }
Where can I find good tutorials about how to do this (...)?
If you want to learn the XSLT language itself, you can check out this previous question: "How to get started with xslt-transformations?".
这篇关于我如何使用XSLT在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!