473,574 Members | 3,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to apply xslt to XmlDocument

Can someone answer this probably obvious question.

I have an xmldocument that i want to apply an xslt file to.
All the examples i find assume u are applying the xslt to an xml file
you've read in.
How can i apply the xslt to an XmlDocument i've just built?

This is wrecking my head, so any code sampe would be greatly
appreciated !

thnak you
Nov 12 '05 #1
3 10960
See this:
http://msdn.microsoft.com/library/en...lTransform.asp.
Includes a working example in two of your favorite languages.

Save your head. Hint for next time: Google on "site:msdn.micr osoft.com .net
xslt xmldocument". The top three items are all relevent.

Cheers,
Stuart Celarier, Fern Creek
Nov 12 '05 #2
Here's a little class to make things easier for you ...

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPat h;
namespace XmlUtils {
/// <summary>
/// Transformer performs an XSLT Transformation.
/// It can be re-used, and is (probably) thread-safe.
/// </summary>
public class XsltTransformer {
private XmlDocument m_objXMLDoc;
private XslTransform m_objXSLTransfo rm;
public XsltTransformer () {
m_objXMLDoc =new XmlDocument();
m_objXSLTransfo rm = new XslTransform();
}
// With an XmlDocument
public void setXML(XmlDocum ent input) {
m_objXMLDoc = input;
}
// With an input stream
public void setXML(Stream input) {
if(input!=null)
m_objXMLDoc.Loa d(input);
}
// With an XML reader
public void setXML(XmlReade r reader) {
if(reader!=null )
m_objXMLDoc.Loa d(reader);
}
// With a plain string
public void setXML(string strXML) {
if(strXML.Lengt h > 0)
m_objXMLDoc.Loa dXml(strXML);
}
public string getXML() {
if(m_objXMLDoc! =null)
return m_objXMLDoc.Inn erXml;
else
return null;
}

// With a plain stringpath
public void setXSL(string strXMLPath) {
if(strXMLPath.L ength > 0)
m_objXSLTransfo rm.Load(strXMLP ath);
}
// With an XmlDocument instance
public void setXSL(XmlDocum ent xsl) {
m_objXSLTransfo rm.Load(xsl.Doc umentElement.Cr eateNavigator() );
}
public void Transform(Syste m.IO.TextWriter output) {
XPathNavigator navigator = m_objXMLDoc.Doc umentElement.Cr eateNavigator() ;
XmlTextWriter writer = new XmlTextWriter(o utput);
m_objXSLTransfo rm.Transform(na vigator, null, writer);
}
public string TransformToStri ng() {
XPathNavigator navigator = m_objXMLDoc.Doc umentElement.Cr eateNavigator() ;
StringBuilder sb = new StringBuilder() ;
StringWriter swriter = new StringWriter(sb );
XmlTextWriter writer = new XmlTextWriter(s writer);
m_objXSLTransfo rm.Transform(na vigator, null, writer);
return sb.ToString();
}
}
}

Angel
O:]
Nov 12 '05 #3
TnaG wrote:
Can someone answer this probably obvious question.

I have an xmldocument that i want to apply an xslt file to.
All the examples i find assume u are applying the xslt to an xml file
you've read in.


What kind of example do you nedd? XslTransform's Transform() method
accepts XmlDocument as is, 'cause XmlDocument is IXPathNavigable .

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4

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

Similar topics

6
5683
by: JJBean | last post by:
Hi Oleg and All, Is this correct? Can I do this? <code> XmlDocument doc=new XmlDocument(); XPathNavigator nav = doc.CreateNavigator(); //Load the xml file. Change path if needed doc.Load(@"..\..\..\books.xml");
0
1256
by: marcum williams | last post by:
Below I'm generating an updategram via xslt and currently referencing schema via <updg:sync mapping-schema="hpXSD.xsd"> .... </updg:sync> But I would like to LOAD the .xsd schema into memory and APPLY to the updategram in memory. Tried using XmlValidatingReader but couldn't get it to work. Thanks
0
2339
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag, such as a div with a runat=server. I can somewhat do this if I create a server control and include the control within the html div tag but this...
8
2127
by: Eric Phetteplace | last post by:
Hello, I am moving over from using msxml2.domdocument to system.xml and system.xml.xsl. I'd like to present this to my group today, so your help is appreciated. I want to transform an XML object (built on the fly) with an XSL document (URL to document) and have it return a string object for my function.
2
4868
by: Wayne Wengert | last post by:
I have an xml file in which I reference an xsl template file to convert the xml to HTML. My system also has VSNET 2003 installed and when I open the xml file in IE6 it opens the file in VSNET instead of processing the file with the stylesheet. How can I get the file to apply the stylesheet? Wayne
4
4812
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt transform via code? Any examples would be great.
7
2844
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID);...
1
1693
by: JJBean | last post by:
How do u apply a XSLT transform to an XmlDocument in c#? Could you please show me this in code ? Thanks, JJBean
1
2366
by: cameron | last post by:
I am attempting to pass in an XmlDocument as a parameter to a transform: from my sample ASPX page: string BaseDir = "/cameron/Play/ComplexParam/"; XmlDocument XSL = new XmlDocument(); XSL.Load(HttpContext.Current.Server.MapPath(BaseDir + "xsl.xsl")); XmlDocument Data = new XmlDocument();
0
7842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7764
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...
0
8110
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. ...
0
8273
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...
1
7862
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...
0
5336
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...
0
3794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1375
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.