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-import-schema.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:import-schema Imports an XML Schema to enable schema-aware processing, typed variable declarations, and validation of input and output documents. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 element <xsl:import-schema namespace="uri" schema-location="file.xsd"/>
xslt
reference
xslt2

Description

xsl:import-schema is a top-level declaration that imports an XML Schema into the stylesheet. Once imported, the schema's type definitions become available for:

  • Type annotations in as attributes on variables, parameters, and functions.
  • Schema-aware matching patterns such as element(*, xs:integer).
  • Input validation via xsl:validate (Saxon-EE).
  • Output validation using the validation attribute on result elements.

Schema-aware processing is an optional feature of XSLT 2.0 and requires a schema-aware processor such as Saxon-EE. Saxon-HE and Saxon-PE do not support schema import.

Parameters

Parameter Type Required Description
namespace URI No The target namespace of the schema to import. Omit for schemas with no target namespace.
schema-location URI No Hint to the processor about where to find the schema document.

Content: may contain an inline xs:schema element instead of using schema-location.

Return value

No direct output. Populates the in-scope schema definitions for the stylesheet.

Examples

Importing a schema and using typed variables

Schema (order.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://example.com/order"
           xmlns:ord="http://example.com/order">
  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="item" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="price" type="xs:decimal" use="required"/>
            <xs:attribute name="qty"   type="xs:integer" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Stylesheet:

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

  <xsl:import-schema namespace="http://example.com/order"
                     schema-location="order.xsd"/>

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

  <xsl:template match="/">
    <xsl:variable name="total" as="xs:decimal"
      select="sum(/ord:order/item/@price * /ord:order/item/@qty)"/>
    <summary total="{$total}"/>
  </xsl:template>
</xsl:stylesheet>

Importing a no-namespace schema inline

Stylesheet:

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

  <xsl:import-schema>
    <xs:schema>
      <xs:element name="record">
        <xs:complexType>
          <xs:attribute name="id"   type="xs:integer" use="required"/>
          <xs:attribute name="name" type="xs:string"  use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  </xsl:import-schema>

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

  <xsl:template match="/record">
    <out id="{@id}" name="{upper-case(@name)}"/>
  </xsl:template>
</xsl:stylesheet>

Notes

  • xsl:import-schema requires a schema-aware XSLT 2.0 processor. Saxon-HE does not support this feature.
  • The schema-location attribute is only a hint; processors may find the schema by namespace alone if they maintain a schema catalog.
  • Importing a schema does not automatically validate the source document. Use the validation attribute on xsl:apply-templates or xsl:result-document for validation.
  • Multiple schemas may be imported by using several xsl:import-schema elements; circular imports are allowed if they mirror valid schema inclusion patterns.

See also