Dynamator Pure HTML for every page generation technology.
           

XML and XSL

Previous examples have used Dynamator to generate JSP that produces HTML. This example uses Dynamator to generate JSP that produces XML.

In general, Dynamator works the same with XML templates as with HTML templates. There are a few differences. The 'id' and 'class' attributes aren't available in XML unless they're declared in the DTD. To keep the template valid XML, the Dynamator file should use the <tag> element to locate elements. If that's not possible, Dynamator can remove the 'id' and 'class' attributes.

The Output

This example produces a sample XML document consisting of a list of terms:

<?xml version="1.0"?>
<terms>
  <term name="epilog"
    The end of the generated file, after the template.
  </term>
  <term name="locator">
    An element in a Dynamator annotations file that identifies
    the set of elements to which a set of overrides will apply.
  </term>
  <term name="marker">
    An invisible HTML element or attribute that identifies a
    location in an HTML file.  The HTML 4.0 markers are
    div, span, id, and class.
  </term>
  <term name="override">
    An element in a Dynamator annotations file that specifies
    a set of changes to be applied to the template.
    (AKA "modifier")
  </term>
  <term name="prolog">
    The beginning of the generated file, before the template.
  </term>
  <term name="template">
    The file containing the static text pattern to be output
    by the generated program.
  </term>
</terms>

XML Template

terms.xml
<?xml version="1.0"?>
<terms>
  <term name="example term">
    The definition of the term.
  </term>
  <!-- div class="Discard" -->
  <term name="another example term">
    The definition of the other term.
  </term>
  <term name="yet another">
    The definition of yet another term.
  </term>
  <!-- /div -->
</terms>

This template uses a Dynamator feature called "comment div". Dynamator treats a comment that begins with the text 'div' as a <div> element, allowing locators to be applied to arbitrary portions of a template document. In this case, the comment div is used to discard elements that are in the template only to provide sample data.

Dynamator File

terms.dyn
<dynamator language="jsp">

<prolog><%@ page session="false" 
%><%@ page contentType="text/plain" 
%><%@ page import="java.util.Hashtable"
%><%!
    private static final Hashtable dictionary = new Hashtable();
    
    static
    {
        dictionary.put(
            "marker",
            "An invisible HTML element or attribute that identifies a "
            + "location in an HTML file.  The HTML 4.0 markers are "
            + "div, span, id, and class.");
        dictionary.put(
            "locator",
            "An element in a Dynamator annotations file that identifies "
            + "the set of elements to which a set of overrides will apply.");
        dictionary.put(
            "prolog",
            "The beginning of the generated file, before the template.");
        dictionary.put(
            "epilog",
            "The end of the generated file, after the template.");
        dictionary.put(
            "override",
            "An element in a Dynamator annotations file that specifies "
            + "a set of changes to be applied to the template.  "
            + "(AKA \"modifier\")");
        dictionary.put(
            "template",
            "The file containing the static text pattern to be output "
            + " by the generated program. ");
    }
%></prolog>

  <tag tag="term">
    <foreach 
        type="Dictionary[String,String]"
        element="entry"
        >
      dictionary
    </foreach>

    <attr name="name">
      <content>entryKey</content>
    </attr>
    
    <content>
      entry
    </content>
  
  </tag>

</dynamator>

Notice that the prolog is formatted to contain no whitespace outside of JSP elements. This formatting ensures that no spaces are inserted into the output before the end of the prolog. In a valid XML document, there are no spaces before the first <? xml ?> tag.

One element added for this demonstration is the JSP page directive specifying contentType="text/plain". Normally, the content type for an XML document would be "text/xml", but this ensures that you will be able to see the XML in your browser.

Processing

Like HTML, XML files are processed using dynamate. Because the input file does not end with a suffix of '.html' or '.htm', dynamate does not preprocess it using Tidy.

.../doc/tutorial/example10_Xml> java dynamate terms.xml

JSP

After processing with Dynamator, the resulting JSP file looks like this:

terms.jsp (generated by Dynamator)

In Action

If you are viewing this page in a servlet engine, you can see the generated xml data.