1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xsl-include.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

4.2 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:include Merges another stylesheet's top-level declarations into the current stylesheet at the same import precedence. 2026-04-18T00:00:00Z 1.0 XSLT 1.0 element <xsl:include href="uri"/>
xslt
reference
xslt1

Description

xsl:include incorporates the top-level declarations of another stylesheet into the including stylesheet as if they had been written there directly. Unlike xsl:import, the included declarations have the same import precedence as the declarations in the including stylesheet. This means there is no override relationship — conflicts between included and including templates follow the same conflict-resolution rules as ordinary duplicates (a processor may signal an error or choose the last declaration).

xsl:include is the right choice when you want to split a large stylesheet into modular files that all operate at the same level. Common patterns include separating formatting templates, utility named templates, and domain-specific rules into distinct files that are included by a master stylesheet.

xsl:include must appear as a top-level child of xsl:stylesheet, and it must appear after all xsl:import elements but before any templates, variable declarations, or other top-level declarations.

Attributes

Attribute Type Required Description
href URI reference Yes Location of the stylesheet to include. Resolved relative to the including stylesheet's base URI.

Examples

Modular stylesheet organisation

utilities.xsl (the included file):

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

  <xsl:template name="upper-first">
    <xsl:param name="text"/>
    <xsl:value-of select="concat(
      translate(substring($text,1,1),
        'abcdefghijklmnopqrstuvwxyz',
        'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
      substring($text,2))"/>
  </xsl:template>

</xsl:stylesheet>

main.xsl (the including stylesheet):

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

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

  <xsl:template match="/words">
    <result>
      <xsl:for-each select="word">
        <item>
          <xsl:call-template name="upper-first">
            <xsl:with-param name="text" select="."/>
          </xsl:call-template>
        </item>
      </xsl:for-each>
    </result>
  </xsl:template>
</xsl:stylesheet>

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<words>
  <word>hello</word>
  <word>world</word>
</words>

Output:

<result>
  <item>Hello</item>
  <item>World</item>
</result>

Including a shared output configuration

output-settings.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" encoding="UTF-8"
    doctype-public="-//W3C//DTD HTML 4.01//EN"/>
</xsl:stylesheet>

page.xsl:

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

  <xsl:include href="output-settings.xsl"/>

  <xsl:template match="/page">
    <html><body><p><xsl:value-of select="."/></p></body></html>
  </xsl:template>
</xsl:stylesheet>

Notes

  • Circular inclusion (A includes B which includes A) is an error. Processors must detect and reject cycles.
  • Because included declarations have the same precedence as the including stylesheet's own declarations, duplicate template rules may cause conflict. Use xsl:import if you want a clear override relationship.
  • xsl:include is resolved at stylesheet load time, not at transformation time; the referenced URI must be accessible when the processor compiles the stylesheet.
  • The included stylesheet's own xsl:output declarations merge with those of the including stylesheet. When there are conflicts, the xsl:output attribute from the stylesheet with higher import precedence wins.

See also