Project

Profile

Help

Error with namespace prefix in XPath

Added by Alexandr Kononov over 9 years ago

Hallo! I am tring compile XSLT template under .NET (C#). If XPath contains namespace prefix - I had an error: 'XPath syntax error at char 12 on line -1 in {root/f:table/f:name}: Undeclared namespace prefix {f}' How can resolve with problem?

My code:


static void Main()
{
	string template = "";

	string tempTemplateFileName = Path.GetTempFileName() + ".xml";
	File.WriteAllText(tempTemplateFileName, template);

	Processor processor = new Processor();

	XsltCompiler compiler = processor.NewXsltCompiler();
	compiler.ErrorList = new ArrayList();
	Uri uri = new Uri(tempTemplateFileName, UriKind.RelativeOrAbsolute);
	compiler.BaseUri = uri;

	XDocument xmlDocument = XDocument.Load(uri.AbsolutePath);
	var reader = xmlDocument.CreateReader();
	try
	{
		XsltExecutable executable = compiler.Compile(reader);
		Console.WriteLine("All right.");
	}
	catch (Exception ex)
	{
		Console.WriteLine("Has error: {0}", ex.Message);
		foreach (object errorInfo in compiler.ErrorList)
		{
			Console.WriteLine(errorInfo);	
		}
	}

	Console.ReadKey();
}

Text from console:

Has error: Failed to compile stylesheet. 1 error detected.
XPath syntax error at char 12 on line -1 in {root/f:table/f:name}:
    Undeclared namespace prefix {f}

Thank you.


Replies (2)

RE: Error with namespace prefix in XPath - Added by Michael Kay over 9 years ago

You need to declare the prefix in your stylesheet, in an ordinary namespace declaration, e.g.

@<xsl:stylesheet xmlns:f="http://some.name/uri" exclude-result-prefixes="f">@

RE: Error with namespace prefix in XPath - Added by Alexandr Kononov over 9 years ago

Thank you very much! Problem resolved. )))

    (1-2/2)

    Please register to reply