Xslt view engine not working with mvc2?

8 views
Skip to first unread message

amyto

unread,
Oct 28, 2009, 3:41:56 PM10/28/09
to mvccontrib-discuss
Hello,

I recently upgraded our project to mvc2 and along with that got the
version of mvccontrib from:

http://github.com/mvccontrib/MvcContrib/archives/mvc2

Everything seems to work great except for the the views that are using
the xslt view engine.
With this version it seems like none of them are getting rendered.
Could there be something I'm possibly missing?
I've updated all the dlls with the ones in the
MvcContrib.XsltViewEngine directory.

Please help,

Amy

Jeremy Skinner

unread,
Oct 28, 2009, 3:59:25 PM10/28/09
to mvccontri...@googlegroups.com
Hi Amy,

I'm not very familiar with the XsltViewEngine, but I just tried putting together a simple MVC2 project and it worked correctly. Are you getting an error message?

Jeremy

2009/10/28 amyto <amy...@gmail.com>

amyto

unread,
Oct 28, 2009, 4:55:31 PM10/28/09
to mvccontrib-discuss
Hi Jeremy,

Nope I don't get any errors. Perhaps it'll help if i try to explain
how I'm using it.

somewhere in Application_Start
...
ViewEngines.Engines.Add(new XsltViewFactory());
...

Now in my controller
...
public ViewResult Greetings(string language)
{
var greetings = new XmlDocument();
var filepath = ControllerContext.HttpContext.Server.MapPath
(FilePaths.Xml.PartnerText);
greetings.Load(filepath);

var data = new XsltViewData();
data.PageVars.Add("language", language.ToLower());
data.DataSources.Add(new XslDataSource(new XmlConvertible
(greetings)));
return View(data);
}
...

greetingtexts.xml:

<?xml version="1.0" encoding="utf-8" ?>
<greetings>
<greeting language="english">
<p>Hello...</p>
</greeting>
<greeting language="spanish">
<p>Hola...</p>
</greeting>
</greetings>

greetings.xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-
prefixes="msxsl">
<xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0
Strict//EN" media-type="application/html+xml" encoding="utf-8" omit-
xml-declaration="yes" indent="no"/>
<xsl:param name="language"></xsl:param>
<xsl:template match="/">
Selected language: <xsl:value-of select="$language"/>
<xsl:copy-of select="//*[@language=$language]/*"/>
</xsl:template>
</xsl:stylesheet>

So the xslt view engine will find the xslt file with the same name as
the action (just like with the normal views unless specified
otherwise) and transform it with the given xml file. I think that's
about it. Also, I misinformed when i said it wasn't rendering, I
meant it wasn't transforming correctly. In the example I gave, all
that shows up is "Selected language", but what you should see if you
pass in the parameter of "english" is "Selected language: english
<p>Hello...</p>".

Again this was working in a version prior to mvc2, I was wondering if
there were some changes i needed to make now with the update.

Much Thanks,


Amy



On Oct 28, 1:59 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Hi Amy,
>
> I'm not very familiar with the XsltViewEngine, but I just tried putting
> together a simple MVC2 project and it worked correctly. Are you getting an
> error message?
>
> Jeremy
>
> 2009/10/28 amyto <amyt...@gmail.com>

Jeremy Skinner

unread,
Oct 28, 2009, 5:06:57 PM10/28/09
to mvccontri...@googlegroups.com
Strange - as far as I remember I didn't make any changes to the XSLT view engine when upgrading to MVC2. I would have expected it to continue working as it did before.

If you could, it would be helpful if you could put together a sample app that reproduces the problem and email it to me. I'll then try and see what's going on.

Thanks

Jeremy

2009/10/28 amyto <amy...@gmail.com>

amyto

unread,
Oct 29, 2009, 11:35:28 AM10/29/09
to mvccontrib-discuss
Jeremy!

Did you get the sample apps?
Anyway, I decided to play with the source code and I was able to get
it to work!
In the XsltView, I added code to the Render method.

public void Render(ViewContext viewContext, TextWriter writer)
{
this.viewContext = viewContext;

string url = viewContext.HttpContext.Request.Url.ToString
();
construct.AppendPage("", url, viewData.PageVars);

var args = new XsltArgumentList();
args.AddExtensionObject("urn:HtmlHelper", new HtmlHelper
(viewContext, this));
args.AddParam("AjaxProScriptReferences", "",
ajaxDeclaration);

//********** Added ************
if (viewData.PageVars != null)
{
foreach (string key in viewData.PageVars.Keys)
{
args.AddParam(key, "", viewData.PageVars[key]);
}
}
//********** end Added ************

xslTransformer.Transform(new XmlInput
(construct.Message.CreateNavigator()), args, new XmlOutput(writer));
}

I don't completely understand what it was adding to the args object,
but it seemed to me like it wasn't getting the arguments I was passing
in so I had to make it add them. Or perhaps I'm still using it wrong?
What do you think?


On Oct 28, 3:06 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Strange - as far as I remember I didn't make any changes to the XSLT view
> engine when upgrading to MVC2. I would have expected it to continue working
> as it did before.
>
> If you could, it would be helpful if you could put together a sample app
> that reproduces the problem and email it to me. I'll then try and see what's
> going on.
>
> Thanks
>
> Jeremy
>
> 2009/10/28 amyto <amyt...@gmail.com>

Jeremy Skinner

unread,
Oct 29, 2009, 11:47:09 AM10/29/09
to mvccontri...@googlegroups.com
Yes, I did get the sample apps - thanks for sending them. I was planning on taking a look at this when I get home from work today.

Jeremy



2009/10/29 amyto <amy...@gmail.com>

amyto

unread,
Oct 29, 2009, 11:49:50 AM10/29/09
to mvccontrib-discuss
Ok thats good. I will check back again later then :)

Amy

On Oct 29, 9:47 am, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Yes, I did get the sample apps - thanks for sending them. I was planning on
> taking a look at this when I get home from work today.
>
> Jeremy
>
> 2009/10/29 amyto <amyt...@gmail.com>

Jeremy Skinner

unread,
Oct 29, 2009, 1:29:38 PM10/29/09
to mvccontri...@googlegroups.com
I had a quick look at this and the code that you posted was spot on - the pageVars are not being copied into the argument list.

This code is missing from both the MVC1 and MVC2 branches and also is not in the current compiled release. It looks like the working version of the XsltViewEngine that you were using in the sample app was quite old, so my guess is this regression was introduced quite some time ago.

I've made the appropriate fixes to both the MVC1 and MVC2 branches, so feel free to give it another try.

Jeremy 

2009/10/29 amyto <amy...@gmail.com>

amyto

unread,
Oct 29, 2009, 6:06:47 PM10/29/09
to mvccontrib-discuss
Thanks so much!

On Oct 29, 11:29 am, Jeremy Skinner <jer...@jeremyskinner.co.uk>
wrote:
> I had a quick look at this and the code that you posted was spot on - the
> pageVars are not being copied into the argument list.
>
> This code is missing from both the MVC1 and MVC2 branches and also is not in
> the current compiled release. It looks like the working version of the
> XsltViewEngine that you were using in the sample app was quite old, so my
> guess is this regression was introduced quite some time ago.
>
> I've made the appropriate fixes to both the MVC1 and MVC2 branches, so feel
> free to give it another try.
>
> Jeremy
>
> 2009/10/29 amyto <amyt...@gmail.com>
Reply all
Reply to author
Forward
0 new messages