473,666 Members | 2,121 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 10964
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
5690
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
1258
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
2347
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 method (borrowed from ASP.NET Website Programming by Wrox press thanks guys) does not give me the full...
8
2129
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
4871
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
4818
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
2852
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); xmlItems.TransformArgumentList = xsltArgList;
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
2367
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
8360
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
8876
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...
1
8556
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,...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.