System.Xml.Xsl.XslTransform and import node

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

Have a common function in Javascript which do transform for all .xsl's.
XSL object is loaded like this.

var xslobj=new ActiveXObject("MSXML2.FreeThreadedDOMDocument.4.0");
xslobj.async = false;
xslobj.load(xslpath);

Here inserting an 'xsl:import' node to the xsl before doing transform.
This xsl contains some common templates which can be used across all xsl's.
So no need of doing import of this specifically in all xsl's.

var xslDoc = xslobj.documentElement;
var newNode =
xslobj.createNode(1,"xsl:import","http://www.w3.org/1999/XSL/Transform") ;
newNode.setAttribute ("href",server.mappath("common.xsl") ;
FirstChild=xslobj.documentElement.childNodes(0);
xslDoc.insertBefore(newNode, FirstChild);

This works fine in Javascript.

Now I'm trying to use the same logic in C Sharp.
Using System.Xml.Xsl.XslTransform for xsl.
Is it possible to do similar thing in C Sharp using
'System.Xml.Xsl.XslTransform' class.

I'm newbie to C Sharp.

thanks,

Praveen.
 
Praveen said:
var xslDoc = xslobj.documentElement;
var newNode =
xslobj.createNode(1,"xsl:import","http://www.w3.org/1999/XSL/Transform") ;
newNode.setAttribute ("href",server.mappath("common.xsl") ;
FirstChild=xslobj.documentElement.childNodes(0);
xslDoc.insertBefore(newNode, FirstChild);

This works fine in Javascript.

Now I'm trying to use the same logic in C Sharp.
Using System.Xml.Xsl.XslTransform for xsl.
Is it possible to do similar thing in C Sharp using
'System.Xml.Xsl.XslTransform' class.

XslTransform is an XSLT processor, not a DOM document. If you want to
manipulate a stylesheet as a DOM document with .NET then you can use
System.Xml.XmlDocument e.g.

XmlDocument stylesheet = new XmlDocument();
stylesheet.Load(xslpath);
XmlElement xslImport = stylesheet.CreateElement("xsl:import",
stylesheet.DocumentElement.NamespaceURI);
xslImport.SetAttribute("href", Server.MapPath("common.xsl");
stylesheet.DocumentElement.InsertBefore(xslImport,
stylesheet.DocumentElement.FirstChild);

Then pass that stylesheet object to the Load method of an XslTransform
instance that takes an IXPathNavigable
<http://msdn2.microsoft.com/en-us/library/ms163479.aspx>

Also note that with .NET 2.0 the preferred XSLT implementation is
XslCompiledTransform, not XslTransform, which should only be used with
..NET 1.x.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top