开发者

XSLT Duplicate Templates with Includes

开发者 https://www.devze.com 2023-02-12 10:59 出处:网络
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 g

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开发者_JS百科 c, but it's easier to manage if the template is in one place.

  • edited: Added actual VS2008 error text.


Using xsl:include is the same as pasting them all in one giant file, which would also give you the same duplicate template errors.

Use xsl:import instead of xsl: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.

  • Have a.xslt xsl:import b.xslt and c.xslt.
  • Have b.xslt and c.xslt xsl:include or xsl:import d.xslt.

Personally, I tend to always use xsl:import over xsl:include.

The only real downside of xsl:import is 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 with xsl: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.


IDEs such as VS2008 tend to believe when you are editing a stylesheet document that it must be complete, that is, taken together with the things it includes/imports, all names must resolve. In fact this is not the case according to the XSLT language; when A includes B it's quite legal for components in B to refer to components in A even though B does not include A. I believe oXygen has a switch somewhere that allows you to control this. XSLT allows cyclic includes, but they can cause problems with some processors.


From http://www.w3.org/TR/xslt#named-templates

It is an error if a stylesheet contains more than one template with the same name and same import precedence.

This also shows that the xsl:import mechanism is better than the inclusion (in most of the cases) and it should be see as the inheritance mechanism between transformations.

0

精彩评论

暂无评论...
验证码 换一张
取 消