Outputting stylesheets (aliases)

Because XSLT documents are XML documents, they can be produced using another XSLT stylesheet. This approach might be used to convert other SGML/XML-based stylesheet languages into XSLT equivalents. Consider the following example stylesheet rule (from a fictional language):

<rule trigger="warning">
  <style type="block" format="bold">
    <trigger-rules/>
  </style>
</rule>

The transformation required needs to convert this into an XSLT template (a quite trivial task in this example), and replace the product-specific formatting information with HTML equivalents (in this case bold and paragraph tags):

<xsl:template match="warning">
  <P><B><xsl:apply-templates/></B></P>
</xsl:template>

The problem that arises is that the output elements 'template' and 'apply-templates' must not get confused with the stylesheet rules. Yet the output document must refer to the XSLT namespace or it will not be later recognized as an XSLT document. At the same time, the XSLT namespace cannot be assigned to the output elements, as this would indicate to the XSLT processor that it should interpret these elements as instructions instead of output elements.

This problem can be overcome by using an alias for the URL used to identify output elements, and the Namespace Alias element to map the alias to the true output namespace. The Stylesheet Prefix and Result Prefix attributes hold the before-and-after names:

<xsl:namespace-alias stylesheet-prefix="alias"
                     result-prefix="xsl" />

This ensures that the correct URL is specified in the output document (the URL for 'xsl' in this case):

<xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:alias="ThisIsAnAlias" >
  ...
  <xsl:namespace-alias stylesheet-prefix="alias"
                       result-prefix="xsl" />
  ...
<!-- CONVERT RULES TO TEMPLATES -->
  <xsl:template match="rule">
    <alias:template match="{@trigger}">
      <xsl:if test="style='block'">
        <P>
          <xsl:if test="format='bold'">
            <B>
              <alias:apply-templates/>
            </B>
          </xsl:if>
          ...
        </P>
      </xsl:if>
      ...
    </alias:template>
  </xsl:template>
  ...
</xsl:stylesheet>

The resulting output for the source rule shown above, and for the whole document, would be as follows:

<alias:stylesheet ...
       xmlns:alias="http://www.w3.org/1999/XSL/Transform">
  ...
  <alias:template match="warning">
    <P><B><alias:apply-templates/></B></P>
  </alias:template>
  ...
</alias:stylesheet>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset