473,654 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transforming XML to HTML using XSL in .NET

Hi all,

I need to transform an xml document, using xsl to a HTML output.

I can do this successfully using the XslTransform class as below:

Dim oTrans As New XslTransform

oTrans.Load(sXS LPath)
oTrans.Transfor m(sXMLPath, "c:\trans.h tm")

However, I need to transform everything from supplied strings. ie the
XML and XSL are provided as inputs (not the paths).

Any suggestions are appreciated. Thanks in advance.

Sergio
Nov 12 '05 #1
3 4871
> I need to transform an xml document, using xsl to a HTML output.
However, I need to transform everything from supplied strings. ie the
XML and XSL are provided as inputs (not the paths). Any suggestions are appreciated. Thanks in advance.

See the code below...... I have avoided doing it with streams, which would
probably be a bit more efficient as using XmlDocument is a bit overkill
here, but this way should be more familiar to you given your past
experience.

Cheers
Chris

-------------------------------------------
Kognition Consulting Limited - Thought Meets Technology
Chris J.T. Auld - Managing Director
Microsoft MVP (Windows Mobile Devices)
Phone: +64 3 453 0064
Mobile: +64 21 500 239
Email: ch***@spam.me.n ot.kognition.co .nz
using System;
using System.IO;
using System.Xml;
using System.Xml.XPat h;
using System.Xml.Xsl;

public class Foo
{
private const String sourceXml= @"<foo>bar</foo>";
private const String sourceXsl= @"<xsl:styleshe et version='1.0'
xmlns:xsl='http ://www.w3.org/1999/XSL/Transform'><xsl :template
match='/'>Your <xsl:value-of select='foo'/>
Here</xsl:template></xsl:stylesheet> ";

public static void Main()
{

XmlDocument xslDoc = new XmlDocument();
xslDoc.LoadXml( sourceXsl);
XslTransform xslt = new XslTransform();
xslt.Load(xslDo c.CreateNavigat or());

XmlDocument contentDoc = new XmlDocument();
contentDoc.Load Xml(sourceXml);
XmlTextWriter writer = new XmlTextWriter(C onsole.Out);
writer.Formatti ng=Formatting.I ndented;

xslt.Transform( contentDoc, null, writer, null);
}
}


Nov 12 '05 #2
Chris J.T. Auld [MVP] wrote:
XmlDocument xslDoc = new XmlDocument();
xslDoc.LoadXml( sourceXsl);
XslTransform xslt = new XslTransform();
xslt.Load(xslDo c.CreateNavigat or());

XmlDocument contentDoc = new XmlDocument();
contentDoc.Load Xml(sourceXml);
XmlTextWriter writer = new XmlTextWriter(C onsole.Out);
writer.Formatti ng=Formatting.I ndented;

xslt.Transform( contentDoc, null, writer, null);


Small nitpicking, sorry:
1. XPathDocument is 30% faster for XSLT than XmlDocument.
2. Transforming to XmlWriter in fact means transforming to XML, not HTML
(the whole xsl:output element is ignored).

Here is my version:

XslTransform xslt = new XslTransform();
xslt.Load(new XmlTextReader(n ew StringReader(so urceXsl)))

XPathDocument doc = new XPathDocument(n ew StringReader(so urceXml));
xslt.Transform( contentDoc, null, Console.Out, null);

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #3
Brilliant! Thanks to both Oleg and Chris. The solutions worked. That
saved me heaps of time.

Sergio
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message news:<O0******* *******@TK2MSFT NGP12.phx.gbl>. ..
Chris J.T. Auld [MVP] wrote:
XmlDocument xslDoc = new XmlDocument();
xslDoc.LoadXml( sourceXsl);
XslTransform xslt = new XslTransform();
xslt.Load(xslDo c.CreateNavigat or());

XmlDocument contentDoc = new XmlDocument();
contentDoc.Load Xml(sourceXml);
XmlTextWriter writer = new XmlTextWriter(C onsole.Out);
writer.Formatti ng=Formatting.I ndented;

xslt.Transform( contentDoc, null, writer, null);


Small nitpicking, sorry:
1. XPathDocument is 30% faster for XSLT than XmlDocument.
2. Transforming to XmlWriter in fact means transforming to XML, not HTML
(the whole xsl:output element is ignored).

Here is my version:

XslTransform xslt = new XslTransform();
xslt.Load(new XmlTextReader(n ew StringReader(so urceXsl)))

XPathDocument doc = new XPathDocument(n ew StringReader(so urceXml));
xslt.Transform( contentDoc, null, Console.Out, null);

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2161
by: Jody Greening | last post by:
Transforming with XSLT, Grouping elements until difference found. I am seeking some help with the following problem, I am fairly new at XSLT transformations, and my problem may lie in looking at it from a traditional programming point of view. I have did quite a bit of searching but have found no answers to my particular problem... please read below XML for the problem I am having.
4
1927
by: Showjumper | last post by:
I am using the NITF DTD for my xml files and i need to use 2 xsl files to do the transform: one for the <body.head> and the second for the <body.content>. I've got this so far for transforming using 1 xsl file: Dim doc As New XmlDocument doc.LoadXml(xmlText) Dim xslDoc As New XslTransform xslDoc.Load(xslpath) Dim sw As New StringWriter xslDoc.Transform(doc, Nothing, sw, Nothing)
5
4496
by: Jiho Han | last post by:
I do a transform of an xml document into another xml using XslTransform. In my xsl file, I specify using <xsl:output encoding="utf-8"/>. However, when my transform is done, the resulting xml is in utf-16. What gives? Anyone?
4
2022
by: Cathie | last post by:
Hi All, I am trying to get my style sheet to work. It works fine in IE but I can't get it to work in .net. Below is the function I use for transforming, where advancedOptionsFile is the path to the file containing an XML document required by the transformation, and xmlSimplified is the XML document to trasform. ---------------------------------------------------------------------------- -------------------------------
4
2213
by: John Bowman | last post by:
Hi, I'm not certain this is the best place to post this, but here it goes. I'm trying to get spaces to work correctly when using the WebBrowser control. Basically, I've got a valid XML document and valid XSL file that I transform into an HTML file using .NET's XmlTransform object. This is all fine except for 1 thing. The XSL has a string of " " codes inside that looks something like:
1
1341
by: Aaron | last post by:
Hello, I want to transform html tags with in the xml file when it use the asp:xml tag, how would I do this? Here is the example of the xslt file: <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
6
4663
by: Adam Clauss | last post by:
I have a list of points (their coming in as decimal types, I can convert to PointF) that I am trying to draw to a form. Unfortunately, the coordinate system these points are coming from is not like Windows Forms use. Instead of (0,0) being the top left, (0,0) is in the "standard" graph position in the center of the coordinate plane. (So we have positive and negative values on both the x- and y- axis). Is there a "good" way to convert...
0
1087
by: Gustaf | last post by:
I'd like to see if someone found a natural explanation to this. I have this method for transforming docs: /** * Transforms an XmlDocument object with an XSL file, and returns * another XmlDocument object. If something bad happens, null is * returned instead. */ private XmlDocument Transform (string xml, string xsl) {
0
1740
by: mannem | last post by:
I have a problem in accessing the values while transforming an XML using XSLT BODY { FONT: x-small 'Verdana'; MARGIN-RIGHT: 1.5em } .c { CURSOR: hand } .b { FONT-WEIGHT: bold; COLOR: red; FONT-FAMILY: 'Courier New'; TEXT-DECORATION: none } .e { MARGIN-LEFT: 1em; TEXT-INDENT: -1em; MARGIN-RIGHT: 1em } .k { MARGIN-LEFT: 1em; TEXT-INDENT: -1em; MARGIN-RIGHT: 1em } .t { COLOR: #990000 } .xt { COLOR: #990099 } .ns { COLOR: red } .dt {...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.