The xsl:namespace-alias Element: Generating Stylesheets

The <xsl: namespace-alias > Element: Generating Stylesheets

One of the primary uses for XSLT is to transform stylesheets into other stylesheets, although that might not be obvious at first. For example, you might want to flesh out long rules that need to be customized just before processing documents. And as you know, XSLT was originally introduced to help create formatting object stylesheets in the first place.

However, this raises an issue: If you process a stylesheet full of elements such as <xsl:template> and <xsl:apply-templates> that you want to appear in the result documentbecause the result document is itself a stylesheethow is XSLT to know how to differentiate those literal result elements from the XSLT elements it should be processing?

This is where the <xsl:namespace-alias> element comes in, because this element enables you to use a new namespace for elements in the source document and convert that namespace back to the correct one in the result document. This element has two attributes:

  • stylesheet-prefix (mandatory). The namespace prefix used in the stylesheet. Set to an NCName or #default.

  • result-prefix (mandatory.) The prefix whose URI to which you want the namespace to be assigned in the result document. Set to an NCName or #default.

The following example shows what I mean. Imagine that this is the stylesheet you want to generate:

 <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">      <xsl:template match="PLANET">          <TR>              <TD>                  <xsl:value-of select="NAME"/>              </TD>              <TD>                  <xsl:apply-templates select="MASS"/>              </TD>              <TD>                  <xsl:apply-templates select="RADIUS"/>              </TD>              <TD>                  <xsl:apply-templates select="DAY"/>              </TD>          </TR>      </xsl:template>  </xsl:stylesheet> 

Note that this stylesheet is full of XSLT elements with the xsl prefix, so if you try to create it using an XSLT transformation, the XSLT processor will try to execute those elements. To avoid that, I give these elements a new namespace prefix, xslt. Heres how that looks in the stylesheet that produces the preceding stylesheet. Note that this stylesheet just matches the root element of the source document so that it can start working; it doesnt actually use the source document for any other purpose:

Listing 9.4 Using <xsl:namespace-alias>
 <xsl:stylesheet version="1.1"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      xmlns:xslt="http://xslt">  <xsl:template match="/">     <xslt:stylesheet version="1.1">      <xslt:template match="PLANET">         <TR>            <TD><xslt:value-of select="NAME"/></TD>            <TD><xslt:apply-templates select="MASS"/></TD>            <TD><xslt:apply-templates select="RADIUS"/></TD>            <TD><xslt:apply-templates select="DAY"/></TD>         </TR>     </xslt:template>     </xslt:stylesheet>  </xsl:template>          .          .          .  </xsl:stylesheet> 

Here, Im using the namespace http://xslt for the xslt prefix, but can change that to the correct XSLT namespace, http://www.w3.org/1999/XSL/Transform, in the output document if I use the <xsl:name space-alias> element:

 <xsl:stylesheet version="1.1"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      xmlns:xslt="http://xslt">  <xsl:template match="/">     <xslt:stylesheet version="1.1">      <xslt:template match="PLANET">         <TR>            <TD><xslt:value-of select="NAME"/></TD>            <TD><xslt:apply-templates select="MASS"/></TD>            <TD><xslt:apply-templates select="RADIUS"/></TD>            <TD><xslt:apply-templates select="DAY"/></TD>         </TR>     </xslt:template>     </xslt:stylesheet>  </xsl:template>  <xsl:namespace-alias stylesheet-prefix="xslt" result-prefix="xsl"/>  </xsl:stylesheet> 

Heres the result. Note that it still uses the namespace prefix xslt, but that namespace now corresponds to the correct XSLT namespace:

 <?xml version="1.0" encoding="UTF-8"?>  <xslt:stylesheet      xmlns:xslt="http://www.w3.org/1999/XSL/Transform" version="1.1">      <xslt:template match="PLANET">          <TR>              <TD>                  <xslt:value-of select="NAME"/>              </TD>              <TD>                  <xslt:apply-templates select="MASS"/>              </TD>              <TD>                  <xslt:apply-templates select="RADIUS"/>              </TD>              <TD>                  <xslt:apply-templates select="DAY"/>              </TD>          </TR>      </xslt:template>  </xslt:stylesheet> 

And thats it for this chapter. The next chapter takes a look at using XSLT in code.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net