Search

I just hit a wall with template matching and priorities. here's an example:

XML:

<data>
    <item/>
    <foo/>
    <bar/>
</data>

Here's main.xsl that includes another helper file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:import href="helper.xsl"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*" mode="test"/>
    </xsl:template>

    <xsl:template match="*" mode="test">
        <xsl:element name="{name()}">
            <xsl:apply-templates select="*" mode="test"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

And helper.xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="item" mode="test" priority="5">
        <item type="this-should-appear-but-it-won't">
            <xsl:apply-templates select="*" mode="test"/>
        </item>
    </xsl:template>

</xsl:stylesheet>

The above prints:

<data>
    <item/>
    <foo/>
    <bar/>
</data>

Instead of:

<data>
    <item type="this-should-appear-but-it-won't"/>
    <foo/>
    <bar/>
</data>

I have set priority to 5 for the rule in helper.xsl but it won't work.

Any ideas why?

Thanks.

Edit: For the moment I'm including helper.xsl instead of importing and it seems to do the trick:

<xsl:include href="helper.xsl"/>

Do you need the xsl:import precedence rule for anything else? Otherwise if you switch to xsl:include this should work as intended (include acts as if the code was in the original doc it's referenced in, import allows the original doc precedence when "overloading" templates).

ref: Apache XML Project

I'm importing another stylesheet just for code organization. So far I was using only xsl:import. Thanks for the tip with xsl:include. It works fine.

Symphony actually used to default to include (pre 2.2.x?), for the "click on utility side bar" functionality but typically most people were getting more use out of the import option. I also use utils for code organisation so I've had to switch it up a few times too.

you might also find useful that you could do <xsl:apply-imports/> to apply the parent version of the same template; for example when you want to use what's in your template and then attach to it whatever was in your utility template.

It's hard that precedence/priority is always a matter of trial and error. Has anybody ever found a tool or technique to display precedences and priorities? Lately I found out that I have a lot of templates with the same priorities (which by definition is an error), but LibXSLT recovers by using the last matching template that is found. So I never see these errors, but still they make maintenance harder because the order of imported templates is important then.

Answering to my own issue above, I am glad to be proven wrong.

Template priority will always be evaluated after the processor has eliminated all template rules with lower import precedence (which is determined by the import tree). This means that a real priority problem could only occur for templates in the same stylesheet (or if you use <xsl:include>). Lucky me. :-)

I should really read those books on XSLT...

BTW: I learned the above from the XSLT 1.0 Pocket Reference.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details