Extension elements

A vendor may add some new instruction elements to those allowed within a template. As these would not be XSLT instructions, they must be defined using a different namespace. For example, a vendor may add an instruction that is able to reverse the characters in a text string, called Reverse:

   <reverse>abcdefghijklmnopqrstuvwxyz</reverse>


<xsl:template match="reverse">
  <P><acme:reverse select="."/></P>
</xsl:template>


   <P>zyxwvutsrqponmlkjihgfedcba</P>

Note that this mechanism only applies within templates. Top-level elements that are not recognized are simply ignored (see below).

Recognizing extension elements

An XSLT processor needs to know which elements are extension elements so that it will not treat these elements as simple output elements. All XSLT processors can be informed that an element is an extension element, rather than an output element, by registering its namespace as an extension namespace. The Extension Element Prefixes attribute may be used on the Stylesheet element, and it holds a space-separated list of namespace prefixes used for extension elements:

<xsl:stylesheet ...
                extension-element-prefixes="acme" >
  ...
  <xsl:template match="reverse">
    <P><acme:reverse select="."/></P>
  </xsl:template>
  ...
</xsl:stylesheet>

Note that the keyword '#default' can be entered in this attribute if the extension elements use the default namespace.

Testing for functionality

When an XSLT processor does not support a particular extension element (developed by another vendor), it can detect and avoid it. Unknown instructions can be avoided by first asking the processor if it understands the instruction, then stepping over the instruction if the processor does not understand it. The function element-available() returns 'true' if the element name passed to it is supported by the XSLT processor. This function can be used in the If and Choose elements:

<P>
  <xsl:if test="element-available('acme:reverse')" >
    <acme:reverse select="." />
  </xsl:if>
  <xsl:if test="not(element-available('acme:reverse'))" >
    <apply-templates/> (NORMALLY REVERSED)
  </xsl:if>
</P>

When the function returns 'false', the following would be output:

<P>abcdefghijklmnopqrstuvwxyz (NORMALLY REVERSED)</P>

Fallback processing

An alternative mechanism embeds 'fallback' instructions within the new instruction itself. If the processor does not understand the instruction, it must implement the instructions embedded within the Fallback elements. Multiple Fallback elements are normally only needed when extension instructions are also embedded within the main instruction. Thus:

<acme:reverse select=".">
  <xsl:fallback><apply-templates/></xsl:fallback>
  <xsl:fallback>(NORMALLY REVERSED)</xsl:fallback>
</acme:reverse>

If the processor recognizes the extended instruction, it ignores the Fallback elements embedded within it.

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

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