Cover Image for XSLT xsl:import
185 views

XSLT xsl:import

In XSLT (Extensible Stylesheet Language Transformations), the xsl:import element is used to import stylesheets from external sources into the current XSLT stylesheet. This allows you to modularize and organize your XSLT code by breaking it into smaller, reusable components that can be imported and used as needed.

The syntax for the xsl:import element is as follows:

<xsl:import href="uri"/>

Here, the href attribute specifies the URI (Uniform Resource Identifier) of the external stylesheet to be imported. The URI can be a relative or absolute path to the stylesheet file or a URL that points to the stylesheet.

When using xsl:import, there are a few important points to consider:

  1. The xsl:import element must be a top-level element in the XSLT stylesheet and should appear before any other elements.
  2. You can have multiple xsl:import elements in a single XSLT stylesheet to import multiple external stylesheets.
  3. The imported stylesheets may define templates, functions, variables, or other XSLT components that can be used within the current XSLT stylesheet.
  4. Elements in the imported stylesheet with xsl:exclude-result-prefixes can be used to exclude specific namespaces from being imported into the current stylesheet.

Here’s a simple example of using xsl:import:

main.xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="helper.xslt"/>

  <xsl:template match="/">
    <output>
      <xsl:value-of select="hello-world()"/>
    </output>
  </xsl:template>
</xsl:stylesheet>

helper.xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:function name="hello-world">
    <xsl:text>Hello, world!</xsl:text>
  </xsl:function>
</xsl:stylesheet>

In this example, main.xslt imports the helper.xslt stylesheet using xsl:import. The hello-world() function defined in helper.xslt can then be used within the main.xslt stylesheet to display “Hello, world!” in the output.

Using xsl:import helps keep your XSLT code organized and promotes code reuse, making XSLT stylesheets more maintainable and modular.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS