Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use XSLT in .NET?

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...

  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?

  2. 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.

  3. 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?

like image 794
SoftwareSavant Avatar asked Jun 09 '11 14:06

SoftwareSavant


People also ask

What is XSLT in asp net?

XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).

What is XSLT in C#?

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents. Sometimes, the user wants some kind of XML structure instead of the whole XML. In that case, we can use XSLT transformation.

How use XSLT is XML?

XSLT stands for Extensible Stylesheet Language Transformation. XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation .

Is XSLT still in use?

XSLT is very widely used. As far as we can judge from metrics like the number of StackOverflow questions, it is in the top 30 programming languages, which probably makes it the top data-model-specific programming language after SQL. But XSLT isn't widely used client-side, that is, in the browser.


1 Answers

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?".

like image 72
R. Martinho Fernandes Avatar answered Sep 16 '22 13:09

R. Martinho Fernandes