Extension Elements

Extension Elements

Extension elements are just what they sound likeelements that have been added to XSLT by the user or vendor. The XSLT 1.1 working draft set up some rules for extension elements, and XSLT 2.0 is supposed to go into a lot more depth on them. In the XSLT 1.1 working draft, the rules were that extension elements must be elements that are user -defined or vendor-defined, but are not top-level elements. And they must belong to a namespace that has been defined as an extension namespace.

You define an extension namespace by using the extension-element-prefixes attribute in an <xsl:stylesheet> element or an xsl:extension-element-prefixes attribute in a literal result element or extension element.

Heres an example. Xalan enables you to create multiple output documents with its <redirect:write> extension element. To use this extension element, I can add a file attribute to the document element in planets.xml, giving the name of a file to which output should be sent, redirected.xml:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS file="redirected.xml">      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

Now, in the XSLT stylesheet, which Ill call redirect.xsl, I define the redirect namespace so that it corresponds to the Java class that supports it in Xalan: org.apache.xalan.lib.Redirect. Ill also set the extension-element-prefixes attribute of <xsl:stylesheet> to the redirect namespace:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      version="1.0"      xmlns:lxslt="http://xml.apache.org/xslt"      xmlns:redirect="org.apache.xalan.lib.Redirect"      extension-element-prefixes="redirect">          .          .          . 

At this point, Im free to use the <redirect:write> extension element to write output to a new file (as opposed to the one specified on the command line). For example, to send the formatted contents of the <PLANETS> element to another file, I can get the filename to create from the file attribute of <PLANETS> and write to that new file as follows :

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      version="1.0"      xmlns:lxslt="http://xml.apache.org/xslt"      xmlns:redirect="org.apache.xalan.lib.Redirect"      extension-element-prefixes="redirect">    <lxslt:component prefix="redirect" elements="write open close" functions="">      <lxslt:script lang="javaclass" src="org.apache.xalan.lib.Redirect"/>    </lxslt:component>    <xsl:output method="xml"/>    <xsl:template match="/">        <xsl:apply-templates/>    </xsl:template>    <xsl:template match="PLANETS">      <redirect:write select="@file">        <PLANETS>          <xsl:apply-templates/>        </PLANETS>      </redirect:write>    </xsl:template>    <xsl:template match="@*node()">      <xsl:copy>        <xsl:apply-templates select="@*node()"/>      </xsl:copy>    </xsl:template>  </xsl:stylesheet> 

Thats all it takes; heres how that might look, using Xalan in Windows:

 C:planets>java org.apache.xalan.xslt.Process -IN planets.xml  -XSL redirect.xsl -OUT new.xml 

This creates the file redirected.xml, which looks like this:

 <?xml version="1.0" encoding="UTF-8"?>  <PLANETS>      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE>      </PLANET>      <PLANET>          <NAME>Venus</NAME>          <MASS UNITS="(Earth = 1)">.815</MASS>          <DAY UNITS="days">116.75</DAY>          <RADIUS UNITS="miles">3716</RADIUS>          <DENSITY UNITS="(Earth = 1)">.943</DENSITY>          <DISTANCE UNITS="million miles">66.8</DISTANCE>      </PLANET>          .          .          . 

Using the element-available Function

You can use the XSLT 1.0 function element-available to test whether an element is available. Heres an example where I check for an element named <starpowder:calculate> :

 <xsl:choose xmlns:starpowder="http://www.starpowder.com">      <xsl:when test="element-available("starpowder:calculate")">          <starpowder:calculate xsl:extension-element-prefixes="starpowder"/>      </xsl:when>      <xsl:otherwise>          <xsl:text>Sorry, can't do math today.</xsl:text>      </xsl:otherwise>  </xsl:choose> 

Theres another way to handle the case where an extension element isnt available: You can use the <xsl:fallback> element.



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