The following code works OK with other files, but when xsl contains <msxsl:script> element XslTransform.Transform Method fails when processing JScript functions.
Any ideas why?
Many thanks
C# code:
string stylesheet = "sample.xsl";
string xmlfile = "sample.xml";
XslTransform xslt = new XslTransform();
xslt.Load(stylesheet);
XmlDocument xpathdocument = new XmlDocument();
xpathdocument.Load(xmlfile);
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting=Formatting.Indented;
xslt.Transform(xpathdocument, null, writer, null);
sample.xml file:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
</catalog>
sample.xsl file:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:teletext="teletext.co.uk" version="1.0">
<msxsl:script language="JScript" implements-prefix="teletext"><![CDATA[
function daysFromPub(nodelistBook) {
var dToday = new Date();
var nodeBook = nodelistBook.item(0);
var sPublishDate = nodeBook.selectSingleNode("publish_date").text;
var nYear = Number(sPublishDate.substr(0,4));
var nMonth = Number(sPublishDate.substr(5,2)) - 1; // months are from 0 to 11
var nDay = sPublishDate.substr(8,2);
var MinMilli = 1000 * 60; //Initialize variables.
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
var dPublish = new Date(nYear, nMonth, nDay);
return String(Math.round((dPublish - dToday) / DyMilli));
}
]]> </msxsl:script>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<td>
Title
</td>
<td>
Days from publication
</td>
</tr>
<xsl:for-each select="//book">
<xsl:sort select="teletext:daysFromPub(.)" data-type="number"/>
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="teletext:daysFromPub(.)"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|