Stylus Studio XML Editor

Table of contents

Appendices

2.7 Embedding Stylesheets

Embedding Stylesheets

Normally an XSLT stylesheet is a complete XML document with the xsl:stylesheet element as the document element. However, an XSLT stylesheet may also be embedded in another resource. Two forms of embedding are possible:

  • the XSLT stylesheet may be textually embedded in a non-XML resource, or
  • the xsl:stylesheet element may occur in an XML document other than as the document element.
  • To facilitate the second form of embedding, the xsl:stylesheet element is allowed to have an ID attribute that specifies a unique identifier.

    NOTE: 

    In order for such an attribute to be used with the XPath id function, it must actually be declared in the DTD as being an ID.

    The following example shows how the xml-stylesheet processing instruction [XMLSTYLE] can be used to allow a document to contain its own stylesheet. The URI reference uses a relative URI with a fragment identifier to locate the xsl:stylesheet element:

    <?xml-stylesheet type="text/xml" href="#style1"?>
    <!DOCTYPE doc SYSTEM "doc.dtd">
    <doc>
    <head>
    <xsl:stylesheet id="style1"
                    version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:import href="doc.xsl"/>
    <xsl:template match="id('foo')">
      <fo:block font-weight="bold"><xsl:apply-templates/></fo:block>
    </xsl:template>
    <xsl:template match="xsl:stylesheet">
      <!-- ignore -->
    </xsl:template>
    </xsl:stylesheet>
    </head>
    <body>
    <para id="foo">
    ...
    </para>
    </body>
    </doc>
    
    NOTE: 

    A stylesheet that is embedded in the document to which it is to be applied or that may be included or imported into an stylesheet that is so embedded typically needs to contain a template rule that specifies that xsl:stylesheet elements are to be ignored.