Elements

The Element element is used to generate element tags. The name of the element to output is specified in the Name attribute:

<element
					name="OL">
  ...
  <!-- CONTENT OF OL ELEMENT -->
  ...
</element>


  <OL>...</OL>

At first sight, this appears to be a less elegant alternative to simply embedding output elements within the stylesheet. Indeed, most people would agree that the stylesheet is less readable when using it. This can be demonstrated by the following examples, which are functionally identical:

<xsl:template match="book">
  <DOC>
    <TITLE>Test</TITLE>
    <MAIN>
      <xsl:apply-templates/>
    </MAIN>
  </DOC>
</xsl:template>


<template match="book">
  <element name="DOC">
    <element name="TITLE">Test</element>
    <element name="MAIN">
      <apply-templates/>
    </element>
  </element>
</template>

However, one good reason for using this technique is that an element can be created with a name that is generated from a computation. This is because the Name attribute may contain an expression. This expression could refer to a variable, or calculate the name of the element to output in some other way (see Chapter 16 for variable examples).

Another good reason for using this element is to avoid the problem that arises when attempting to use a DTD-sensitive XML editor to create the stylesheet. Incorporating output elements into the document requires that the DTD be updated to allow them to occur (and it is not sufficent simply to copy the original element and attribute declarations into the XSLT DTD, as their content models must be changed to allow embedded XSLT elements). When formatting the stylesheet on-screen to assist with authoring, it would also be necessary to create display formats for each of the elements. But the Element element is already part of the XSLT DTD. A reasonable editor will allow the Name attribute value to be presented at all times (as a prefix), at least partly overcoming the legibility issue:

Finally, as the comparitive examples above demonstrate, there is no need for namespaces when using this technique.

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

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