When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown function."
It was annoying because I had checked created the whole document and tested it in Internet Explorer 6.0 and everything worked perfectly; no errors or warnings.
After doing research i found out that most often times the error that i was receiving should only be given upon a Transformation or Select Node and is solved by providing an XSLtcontext to resolve custom functions and in some cases functions like position etc.
After alot of debugging i pinned it down to one section [( position() > (( $cur-page ) * $no-rows * $no-cols) )]" (note I have since shortened it as it was about 3 times longer then this. I just identified this as the "offending section". Now one thing that I will note there is an extra pair of Parentheses around $cur-page. As i rememember this was done because at some time during the creation process I received a strange error that was similar to the "unknown" function and for some reason putting the parentheses around it solved the problem and thus was forgotten.
changing
[( position() > (( $cur-page ) * $no-rows * $no-cols) )]" - which is Perfectly valid in IE 6.0 and Mozilla/Firebird was causing the Exception to be thrown. After Playing around with it a bit as i had a piece of logic very similar to this one that was perfectly fine i went and put - 0 in and it worked fine
so
[( position() > (( $cur-page ) * $no-rows * $no-cols) )]" - Fine in IE Errors in C#.NET
[( position() > ( $cur-page * $no-rows * $no-cols) )]" - Fine in IE - fine in C#
[( position() > (( $cur-page - 0 ) * $no-rows * $no-cols) )]" - Fine in IE - Fine in C#
In Conclusion: I do not know if this is a design by nature, a bug or what not. All i know is it caused me a great deal of headache trying to figure it out. And as i stated before while the extra parantheses were not needed they were there due to another strange error that I had been getting earlier in the stage of developing.
Here is a Sample XSLT File
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:http="urn:extended-xlt" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="no-cols" select="root/Images/@across" />
<xsl:param name="no-rows" select="root/Images/@down" />
<xsl:param name="cur-page" select="root/CurrentPage/@value" />
<xsl:template match="/">
<xsl:apply-templates select="root/Images/file[( position() > (( $cur-page ) * $no-rows * $no-cols) )]" mode="new-row"/>
</xsl:template>
<xsl:template match="file" mode="new-row">
<tr>
<xsl:apply-templates select=". | following-sibling::file[position() < $no-cols]"/>
</tr>
</xsl:template>
<xsl:template match="file">
<td>
<a href="#" title="|{@name}|"><img src="Snapshot - MSN Messenger Beta.JPG" /></a>
</td>
</xsl:template>
</xsl:stylesheet>
Sample C# Code being used
public void FirstTrial(string xsltLocation)
{
try
{
XslTransform tmpXslt = new XslTransform();
XPathDocument tmpxpathDoc = new XPathDocument(xsltLocation);
clsXmlResolver ovrXmlResolver = new clsXmlResolver();
tmpXslt.Load(tmpxpathDoc, ovrXmlResolver, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
FirstTrial("template.xslt");
}