Sponsored

XSLT Reference

xsl:override

XSLT 3.0 element

Inside xsl:use-package, provides local implementations that replace specific components imported from the used package.

Syntax
<xsl:override>

Description

xsl:override is a child element of xsl:use-package that contains one or more locally-defined component declarations—templates, functions, variables, or attribute sets—that replace components of the same name imported from the used package. It is the mechanism for customising or extending a package without modifying the package source.

A component in the used package can be overridden only if its visibility is public or abstract (not final or private). When an override is provided for an abstract component, the local definition satisfies the package’s requirement for an implementation. When an override is provided for a public component, the local definition takes precedence over the package’s version.

Within the overriding implementation, the original package component can be called using xsl:original, which refers to the superseded version, enabling a decorator pattern where local code augments rather than entirely replaces the original.

Parameters

xsl:override takes no attributes. Its content is one or more component declarations (e.g., xsl:template, xsl:function, xsl:variable) whose names match components exposed as public or abstract by the used package.

Return value

xsl:override is a declaration; it produces no XDM value.

Examples

Overriding a public function to change its behaviour

Assume package com.example.pricing exposes a public function pricing:apply-discount#2.

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:pricing="com.example.pricing">

  <xsl:use-package name="com.example.pricing" package-version="1.*">
    <xsl:override>
      <!-- Replace the discount function with a doubled-discount version -->
      <xsl:function name="pricing:apply-discount" as="xs:decimal">
        <xsl:param name="price"    as="xs:decimal"/>
        <xsl:param name="discount" as="xs:decimal"/>
        <!-- Call the original with double the discount, capped at 50% -->
        <xsl:sequence select="xsl:original($price, min(($discount * 2, 0.5)))"/>
      </xsl:function>
    </xsl:override>
  </xsl:use-package>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/catalog">
    <sale>
      <xsl:for-each select="item">
        <item name="{@name}">
          <xsl:value-of select="format-number(pricing:apply-discount(@price, 0.1), '0.00')"/>
        </item>
      </xsl:for-each>
    </sale>
  </xsl:template>
</xsl:stylesheet>

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <item name="Widget" price="100"/>
  <item name="Gadget" price="200"/>
</catalog>

Output (original function applies 10% discount; override applies 20%):

<sale>
  <item name="Widget">80.00</item>
  <item name="Gadget">160.00</item>
</sale>

Implementing an abstract template required by a layout package

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

  <xsl:use-package name="com.example.layout" package-version="1.0">
    <xsl:override>
      <!-- Satisfy the abstract render-header contract -->
      <xsl:template name="render-header">
        <header>
          <nav>
            <a href="/">Home</a>
            <a href="/about">About</a>
          </nav>
        </header>
      </xsl:template>
    </xsl:override>
  </xsl:use-package>

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <xsl:call-template name="render-page"/>
  </xsl:template>
</xsl:stylesheet>

Output:

<html>
  <header>
    <nav>
      <a href="/">Home</a>
      <a href="/about">About</a>
    </nav>
  </header>
  <body/>
</html>

Notes

  • Only components with public or abstract visibility in the used package may appear inside xsl:override. Attempting to override a final or private component is a static error.
  • xsl:original is a pseudo-instruction available inside an overriding component that delegates to the original package implementation. It uses the same calling syntax as the normal call (e.g., xsl:original as a function call for functions, or <xsl:call-template name="xsl:original"/> for templates).
  • Overrides in xsl:override are not subject to import precedence; they unconditionally replace the package component.
  • A single xsl:use-package may contain at most one xsl:override element, but that element may contain multiple component declarations.

See also