views:

119

answers:

4

Hi all:

Currently I have an xml structure in an app. I needed to convert it from one structure to another. I have the xsds for it. The app is in C#. Naturally I thought of using good old coding to convert it, but that sounded like the least efficient idea. Someone recommended me to use XSLT, but I'm not 100% sure how it works.

Does anyone know how to use XSLT to convert an xml structure to another? Examples would be Nice. Or are there any other free applications that would do a better job?

Thanks.

+1  A: 

You can use the XslCompiledTransform class to invoke XSLT. The documentation has lots of examples.

Mark Seemann
So basically I need a xsl to define what has changed from the old xml, replace the old tags with the new ones, rinse and repeat?
BeraCim
That sounds right.
Mark Seemann
Sorry I'm a bit confused. I used to use XSL to display some table values from xml and such. I googled XSLT and yes it does say that it can transform XML into another XML, but usually at the end of an XSL file it comes down to displaying HTML data. How do I get it to display xml instead?
BeraCim
`<xsl:output mode="xml"/>` I think. OBTW, it doesn't "display" anything. It outputs stuff.
John Saunders
XslCompiledTransform had a bit of trouble accessing server paths. I had to manually string together the path to a server in order to get it working because Server.MapPath did not work. Though it had a bit of problems writing to a server directory, generally it did the job. Thanks.
BeraCim
A: 

Support your friendly book author. I read about this topic this week in the book Pro LINQ. XSLT is something I never considered using, but I think I might be needed it pretty soon for XML conversions.

Dave
A: 

Have a look at: XslCompiledTransform Class


        using (XmlWriter myWriter =  XmlWriter.Create("result.html"))
        {
            string xmlPath = ""; // xml to transform
            string xslPath = ""; // xsl path

            XPathDocument myXPathDoc = new XPathDocument(xmlPath);
            XslCompiledTransform xslTrans = new XslCompiledTransform();

            //load the Xsl 
            xslTrans.Load(xslPath);

            //do the actual transform of Xml
            xslTrans.Transform(myXPathDoc, null, myWriter);
        }
Asad Butt
-1: for using `XmlTextWriter`. I'd downvote twice if I could, since you're not using a `using` block! This code sets several bad examples.
John Saunders
@Asad: do not use `XmlTextWriter`, unless you're using .NET 1.1. Since .NET 2.0, you should be using `XmlWriter.Create`.
John Saunders
Thanks mate, have fixed. Do appreciate.
Asad Butt
A: 
  1. This is what XSLT is for. The XML-to-HTML examples you're finding are really a special case. A very useful special case, but even so.
  2. XSLT is not insanely difficult to learn, if you have a good book. Michael Kay's XSLT Programmer's Reference used to be definitive, but I haven't needed a book on XSLT for years so I don't know if it still is.
Robert Rossney
Anything on XSLT from Michael Kay is going to be definitive.
John Saunders
I can't imagine otherwise. I just thought he might have written another book since then.
Robert Rossney