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, or XmlReader
and XmlWriter
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?".