using xsl:stream of XSLT 2.1
I have a javascript which converts which applies xsl to an xml. I need to use XSLT 2.1 in order to use the feature xsl:stream.
Now the script is as follows :
main();
function main()
{
if ( WScript.Arguments.length != 3 )
{
WScript.Echo("Usage: runTransform.js <xslfilename> <xmlfilename> <outputfilename>");
WScript.Quit();
}
var xslfilename = WScript.Arguments.Item(0);
var xmlfilename = WScript.Arguments.Item(1);
var outputfilename = WScript.Arguments.Item(2);
var doc = LoadDOM(xmlfilename);
var xsl = LoadDOM(xslfilename);
var str = doc.transformNode(xsl);
var ado = new ActiveXObject("ADODB.Stream");
ado.Open();
ado.Position = 0;
ado.CharSet = "UTF-8";
ado.WriteText(str);
ado.SaveToFile(outputfilename, 2)
}
function LoadDOM(file)
{
var dom;
try {
dom = MakeDOM(null);
dom.load(file);
}
catch (e) {
alert("error" + e.description);
}
return dom;
}
function MakeDOM(progID)
{
if (progID == null) {
progID = "msxml2.DOMDocument.4.0";
}
var dom;
try {
WScript.Echo("ProgID is dom 4");
dom = new ActiveXObject(progID);
dom.async = false;
dom.validateOnParse = false;
//dom.resolveExternals = false;
}
catch (e) {
alert("MakeDom Error :" + e.description);
}
return dom;
}
function alert(str)
{
WScript.Echo(str);
}
The sample xml that I am trying is as follows :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<transactions>
<transaction value="12.51"/>
<transaction value="3.99"/>
</transactions>
The xsl to be applied is as follows :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="2.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:mode streamable="yes" />
<xsl:stream href="E:\test_folder_stream\transactions.xml">
<count>
<xsl:value-of select="count(transactions/transaction)" />
</count>
</xsl:stream>
</xsl:stylesheet>
The output which I get is as follows :
<?xml version="1.0"?>
Ideally I should get the output as :
<?xml version="1.0">
<count>2</count>
The script is executed as follows :
test.js transactions.xsl transactions.xml output.xml
The script does开发者_运维问答 not throw any error . Is the xsl that I have written incomplete ? Any suggestions or links will be valuable .
Thanks in advance Tazim.
I don't think there is any XSLT processor currently available which implements xsl:stream
.
Saxon 9.3 implements many of the streaming features in the XSLT 2.1 (now XSLT 3.0) working draft, but not yet the xsl:stream instruction. I suspect you are using an XSLT 1.0 processor. If you specify version="2.1" in a stylesheet and throw it at an XSLT 1.0 processor, it will ignore any children of the xsl:stylesheet element that it doesn't understand.
In fact xsl:stream
is defined in XSLT 3.0 as an instruction, so it can't appear as a child of xsl:stylesheet
- it needs to go in a function or template.
If you need streamed processing, get yourself a copy of Saxon 9.3 Enterprise Edition, and read the documentation carefully to see what subset of the working draft is currently implemented. You won't be able to use it from JavaScript, unfortunately.
精彩评论