Question

Say you have these two xsl files:

cow-wrapper.xsl

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="cow">
            <next-match />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

test.xsl

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:import href="cow-wrapper.xsl" />

    <xsl:template match="/">
        <!-- regular stuff to do -->
    </xsl:template>

</xsl:stylesheet>

In this case the root match in cow-wrapper.xsl is not called at all. Is there a way to make the root template match in cow-wrapper.xsl have presedence over the one in test.xsl?

What I'm after is a way to simply import a file and have it wrap the regular output. For example in a soap envelope.

Was it helpful?

Solution 2

From the XSLT spec, it looks like imported templates always have lower precedence than conflicting templates with higher import precedence (where the main XSLT would have the highest import precedence), but perhaps this is a suitable workaround?:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="cow">
            <apply-templates select="." mode="regular">
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:import href="cow-wrapper.xsl" />

    <xsl:template match="/" mode="regular">
        <!-- regular stuff to do -->
    </xsl:template>

</xsl:stylesheet>

OTHER TIPS

If I want the template in the imported template to be "executed" first, I would either not have a template in the importing stylesheet with the same match pattern, or I would write it this way:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:import href="cow-wrapper.xsl" />

    <xsl:template match="/">
        <xsl:next-match/>
        <!-- regular stuff to do -->
    </xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top