By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

BUG in xsltTransform.Load: XsltContext is needed for this query because of an unknown function.

P: n/a
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() &gt; (( $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() &lt; $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");

}

Nov 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Torrent wrote:
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." *


I have managed to simplify your bug case to just

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:variable name="var" select="3"/>
<xsl:value-of select="($var)*2"/>
</xsl:template>
</xsl:stylesheet>

Runnig under 1.0 and 1.1 it throws:
System.Xml.Xsl.XsltCompileException: (4,10) :
---> System.Xml.Xsl.XsltException: '($var)*2' is an invalid XPath
expression. ---> System.Xml.XPath.XPathException: XsltContext is needed for
this query because of an unknown function.
at System.Xml.XPath.VariableQuery.ReturnType()
at System.Xml.XPath.GroupQuery.ReturnType()
at System.Xml.XPath.NumericExpr..ctor(Op op, IQuery opnd1, IQuery opnd2)
at System.Xml.XPath.QueryBuilder.ProcessOperator(Oper ator root, IQuery
qyInput, Boolean& cache, Boolean& position)
at System.Xml.XPath.QueryBuilder.ProcessNode(AstNode root, IQuery qyInput,
Int32 parent, AxisType parentaxis, Boolean& cache, Boolean& position)
at System.Xml.XPath.QueryBuilder.Build(AstNode root, String query)
at System.Xml.Xsl.Compiler.AddQuery(String xpathQuery, Boolean allowVar,
Boolean allowKey)
--- End of inner exception stack trace ---
at System.Xml.Xsl.Compiler.AddQuery(String xpathQuery, Boolean allowVar,
Boolean allowKey)
at System.Xml.Xsl.ValueOfAction.CompileAttribute(Comp iler compiler)
at System.Xml.Xsl.CompiledAction.CompileAttributes(Co mpiler compiler)
at System.Xml.Xsl.ValueOfAction.Compile(Compiler compiler)
at System.Xml.Xsl.Compiler.CreateValueOfAction()
at System.Xml.Xsl.ContainerAction.CompileInstruction( Compiler compiler)
at System.Xml.Xsl.ContainerAction.CompileOnceTemplate (Compiler compiler)
at System.Xml.Xsl.ContainerAction.CompileTemplate(Com piler compiler)
at System.Xml.Xsl.TemplateAction.Compile(Compiler compiler)
at System.Xml.Xsl.Compiler.CreateTemplateAction()
at System.Xml.Xsl.ContainerAction.CompileTopLevelElem ents(Compiler compiler)
at System.Xml.Xsl.ContainerAction.CompileDocument(Com piler compiler,
Boolean inInclude)
at System.Xml.Xsl.RootAction.Compile(Compiler compiler)
at System.Xml.Xsl.Compiler.CreateRootAction()
at System.Xml.Xsl.Compiler.Compile(NavigatorInput input, XmlResolver
xmlResolver)
--- End of inner exception stack trace ---
at System.Xml.Xsl.Compiler.Compile(NavigatorInput input, XmlResolver
xmlResolver)
at System.Xml.Xsl.XslTransform.Compile(XPathNavigator stylesheet,
XmlResolver resolver)
at System.Xml.Xsl.XslTransform.Load(XmlReader stylesheet)
at Test.Main(String[] args) in d:\projects\test2\class1.cs:line 28
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2

P: n/a
Oleg Tkachenko wrote:
Torrent wrote:
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." *

I have managed to simplify your bug case to just

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:variable name="var" select="3"/>
<xsl:value-of select="($var)*2"/>
</xsl:template>
</xsl:stylesheet>


Forgot to say: a workaround is to remove (superfluous anyway) parentheses
around a variable reference:
$cur-page * $no-rows * $no-cols
instead of
( $cur-page ) * $no-rows * $no-cols
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #3

P: n/a
Yep. As I pointed out. I only took the time to post it afterwards because
even though they were superflous and after the whole thing was done it still
imho is a bug
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:Oh****************@TK2MSFTNGP09.phx.gbl...
Oleg Tkachenko wrote:
Torrent wrote:
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." *

I have managed to simplify your bug case to just

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:variable name="var" select="3"/>
<xsl:value-of select="($var)*2"/>
</xsl:template>
</xsl:stylesheet>


Forgot to say: a workaround is to remove (superfluous anyway) parentheses
around a variable reference:
$cur-page * $no-rows * $no-cols
instead of
( $cur-page ) * $no-rows * $no-cols
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.