I’m having a problem with including xslt templates.
I have a.xslt which includes b.xslt and c.xslt.
b and c both require a template located in d.xslt. If I add the include statement in b and c, I get a duplicate template error in VS2008:
The named template 'MyTemplate' does not exist.
and when I try to hit the web page that uses these XSLTs I get an error and they don’t display correctly.
If I include d.xslt in a.xslt it will display correctly, but I get an error in b and c stating that the template I’m referencing doesn’t exist:
'MyTemplate' is a duplicate template name.
What would be the correct way to have this kind of include tree? Or maybe it’s just a VS2008 problem?
I could eliminate d.xslt and add that template to both b and c, but it’s easier to manage if the template is in one place.
- edited: Added actual VS2008 error text.
Using
xsl:includeis the same as pasting them all in one giant file, which would also give you the same duplicate template errors.Use
xsl:importinstead ofxsl:include.It will overlay/merge all of the templates to give you a super-set. The last template in the import chain will “win” instead of giving you a duplicate definition error, as it will have higher precedence.
xsl:importb.xslt and c.xslt.xsl:includeorxsl:importd.xslt.Personally, I tend to always use
xsl:importoverxsl:include.The only real downside of
xsl:importis that you might accidentally override a template further down in the import chain and not know it(because you won’t get the same compilation error that you would withxsl:include). There might be a slight performance hit, since the XSLT processor has to “think” a little more about the import chain, but I haven’t found that to be a problem.