Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


XML/XSL streamed IHTMLDocument2

Posted on 2003-03-16
Medium Priority
Last Modified: 2013-12-03
I have a web browser control. In my application, I want to generate XML documents (in memory) and inject them into the control using pre defined XSL sheets. I've tried a simple code:

<?xml-stylesheet type="text/xsl" href="d:/test.xsl"?>

I've also put a file called text.xsl on D:\.
I have the following code:

void CMainView::Load(IStream* pStream)
     CComPtr<IDispatch> pDispatch;
     CComQIPtr<IHTMLDocument2> pDocument(pDispatch);
     CComQIPtr<IPersistStreamInit> pPersistInit(pDocument);

void CMainView::Load(LPCTSTR szText)
     DWORD dwLength=lstrlen(szText);
     HGLOBAL hHandle=GlobalAlloc(GPTR,dwLength);
     CComPtr<IStream> pStream;
     if (SUCCEEDED(CreateStreamOnHGlobal(hHandle,TRUE,&pStream)))

When I try to load the XML using the functions, I can see the entire code in the control instead the processed output (using the xsl). If I try to navigate the XML file (using Navigate/Navigate2), everything in OK. I DON'T want to use navigate because all the XML creation is made in memory and I don't want to save it to disk. How can I inject it directly into the document?
Question by:gilad_no
  • 4
  • 2
LVL 49

Expert Comment

ID: 8147492
I think you are looking for the transformNodeToObject  method of the XMLDOMDocument object.  The output is a stream or an XMLDOMDocument.  I'll see if I can cobble together and example
-- Dan
LVL 49

Accepted Solution

DanRollins earned 1200 total points
ID: 8147680
First let me say that C++ is absolutely the hardest language to use when working with COM objects.  I started creating an example for you, but I thought of a better way to show the basics.  JScript makes the object creation easy.  So here are three files for you

=-=-=-=-=-=-=-=-=- start of file Data.xml
<?xml version="1.0" encoding="UTF-8" ?>

=-=-=-=-=-=-=-=-=- end of file

=-=-=-=-=-=-=-=-=- start of file Style.xml
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<COMMENT> using &#160; for hardspace </COMMENT>
<H2><CENTER>Here is the output!<SUP><FONT SIZE="-1">TM</FONT></SUP> </CENTER></H2>

<TABLE border="2">
<xsl:for-each select="/Data/Person">
<TR><TD ALIGN='right'>Name:</TD>    <TD><xsl:value-of select="Name"/> </TD></TR>
<TR><TD ALIGN='right'>Age:</TD>     <TD><xsl:value-of select="Age"/>  </TD></TR>
=-=-=-=-=-=-=-=-=- end of file

=-=-=-=-=-=-=-=-=- start of file Tester.HTA
function DoClick() {
     // load data
     var oXml= new ActiveXObject("Microsoft.XMLDOM");
     oXml.async = false;

     // load style sheet
     var oSheet= new ActiveXObject("Microsoft.XMLDOM");
     oSheet.async= false;

     // set up the resulting document
     var oOutput= new ActiveXObject("Microsoft.XMLDOM");
     oOutput.async= false;
     oOutput.validateOnParse= true;

     // parse results into a result DOM Document
     oXml.transformNodeToObject( oSheet, oOutput);  

     //var s= oOutput.nodeValue;
     //var s= oOutput;
     var s= oOutput.documentElement.xml;

<input type=button value="Click me" onclick="DoClick();">
<input type=button value="Reload" onclick="document.location.reload();">

=-=-=-=-=-=-=-=-=- end of file

Create the three files adn then double-click the HTA file.
The procedure is to create three XMLDOMDocument objects.  Load one with the data and one with the sytle sheet.  Then use transformNode to apply the stylesheet to the XML and output into the third.  As an example, I'm just spewing the resulting XML as text into an alert box, but you could insert it into a DIV or stream it itno a new HTML Document or whatever.

If you need doing either of these latter options, or if you need help translating the JScript into C++, please ask  -- I'd like a chance to earn an A on this Question. Thanks.

-- Dan

Author Comment

ID: 8150305
I've tried to use transformNode instead, but it doesn't work.

IXMLDOMDocumentPtr pDocument,pSheet;

// szText contains the xml data

// szSheet contains the xsl sheet

IXMLDOMNodePtr pRoot=pDocument->GetdocumentElement();
IXMLDOMNodePtr pRootSheet=pSheet->GetdocumentElement();


In the last statement, I always recieve an empty string.
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.


Author Comment

ID: 8150463
It works!



CString szOutput((LPCTSTR)(_bstr_t)pDocument->transformNode(pSheet));
LVL 49

Expert Comment

ID: 8150469
Greate! You are almost there!   Please look a little more closely at my post.  The final few lines create a THIRD xml document object and use it in a call to the
member function.  I'm confident that if you take that last simple step, you will be rewarded with a great measure of satisfaction!  Happiness is within your grasp!

-- Dan
LVL 49

Expert Comment

ID: 8150484
Joy ripples though my entire being at your success.
-- Dan

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question