Re: [xsl] writing to file from MSXML 4.0

Subject: Re: [xsl] writing to file from MSXML 4.0
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Fri, 6 Jun 2008 14:41:26 +0100
--------------------------------------------------
From: "Christian Wittern" <cwittern@xxxxxxxxx>
Sent: Friday, June 06, 2008 2:10 PM
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] writing to file from MSXML 4.0

Dear Joe,

Thanks for the quick answer.

Chris

Basically you use transformNodeToObject, there's an example here:
http://msdn.microsoft.com/en-us/library/ms766561(VS.85).aspx
If your output is XML then use the same logic as the example except
instead of the last line (WScript.echo) use result.save(<path to >
save to>).
If the output is not XML you can use an ADODB.Stream instead of a
DomDocument to accept the result and write to disk.

I guess you are talking about this example:


// Load data. var source = new ActiveXObject("Msxml2.DOMDocument.3.0"); source.async = false; source.load("hello.xml"); if (source.parseError.errorCode != 0) { var myErr = source.parseError; WScript.Echo("You have error " + myErr.reason); } else { // Load style sheet. var stylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0"); stylesheet.async = false; stylesheet.load("hello.xsl"); if (stylesheet.parseError.errorCode != 0) { var myErr = stylesheet.parseError; WScript.Echo("You have error " + myErr.reason); } else { // Set up the resulting document. var result = new ActiveXObject("Msxml2.DOMDocument.3.0"); result.async = false; result.validateOnParse = true; // Parse results into a result DOM Document. WScript.Echo(source.transformNodeToObject(stylesheet, result)); } }

Unfortunately, this is doing the reverse, calling a XSL transform from
Jscript. What I need is calling JScript from XSLT.
The following is the closest I found to what I need:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:user="http://mycompany.com/mynamespace";>

<msxsl:script language="JScript" implements-prefix="user">
  function xml(nodelist) {
     return nodelist.nextNode().xml;
  }
</msxsl:script>

<xsl:template match="/">
  <xsl:value-of select="user:xml(.)"/>
</xsl:template>

</xsl:stylesheet>

However what I do not understand is, how to get an object that can be
saved in the msxsl:script block.

All the best,

Chris


Chris

You're right, I misunderstood.
In the script block you need to create a new DomDocument and load the XML. Then call the save method.
function saveNodes(nodelist)
{
var oDom = new ActiveXObject("MSXML2.DomDocument.4.0");
oDom.loadXML("<root/>");
for (var i = 0, l = nodelist.length; i < l; i++)
{
var node = nodelist[i];
oDom.documentElement.appendChild(node.cloneNode(true));
}
oDom.save("myFolder/myFile.xml");
}


Joe
http://joe.fawcett.name


Current Thread