Sign In/My Account | View Cart  
advertisement

Article:
 XSLT Processing in .NET
Subject: XslTransform.Transform Method doesn't work when xsl contains
Date: 2003-06-18 08:58:59
From: Tanya Gillingham

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>


Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • XslTransform.Transform Method doesn't work when xsl contains
    2005-08-15 07:26:20 shsamuel

    Tanya,


    When something fails, it's most useful to see the particular exception it generated. In absence of that, I'll take a guess at what looks like an good reason it might have failed.


    You seem to be missing an XmlResolver and Evidence. The short signature Load and Transform methods have been deprecated (possibly since your original post) to force you to include resolvers and evidence. Resolvers resolve eternal references within your XML, and Evidence gives policy-based rights to process scripts. The documentation is a little sparse, but based on Microsoft's normal ways of doing things, default (i.e. -- null) evidence is probably of the level that will ignore your JScript because the system things it's untrusted.


    If your examples are verbatim and complete, you can probably get away without a resolver: you don't have any external references. Try an explicit System.Security.Policy.Evidence from Assembly.Evidence of your executing assembly (probably easiest as this.GetType().Assembly.Evidence) to give green light security to any code. Bear in mind that this grants any XSL that you run through that code full policy access, so make sure your code won't break anything and comes from a trusted source. Alternately, insist that your source provides evidence and authenticate against that.



Sponsored By: